Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created April 1, 2015 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/b913ea0b1eab6ea0d84c to your computer and use it in GitHub Desktop.
Save shigemk2/b913ea0b1eab6ea0d84c to your computer and use it in GitHub Desktop.
data TrafficLight = Red | Yellow | Green
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
-- aはなんでもいいので、Treeでもリストでも本当になんでもいい。とにかく値が入っていればいい感じのJSっぽい感じをHaskellで表現したい
class YesNo a where
yesno :: a -> Bool
instance YesNo Int where
yesno 0 = False
yesno _ = True
instance YesNo [a] where
yesno [] = False
yesno _ = True
instance YesNo Bool where
yesno = id
instance YesNo (Maybe a) where
yesno (Just _) = True
yesno Nothing = False
instance YesNo (Tree a) where
yesno EmptyTree = False
yesno _ = True
instance YesNo TrafficLight where
yesno Red = False
yesno _ = True
yesnoIf :: (YesNo y) => y -> a -> a -> a
yesnoIf yesnoVal yesResult noResult =
if yesno yesnoVal
then yesResult
else noResult
main = do
print $ yesno $ length []
print $ yesno "haha"
print $ yesno ""
print $ yesno $ Just 0
print $ yesno True
print $ yesno EmptyTree
print $ yesno []
print $ yesno [0,0,0]
print $ yesnoIf [] "YEAH!" "NO!"
print $ yesnoIf [2,3,4] "YEAH!" "NO!"
print $ yesnoIf True "YEAH!" "NO!"
if(0) {
console.log("yes");
} else {
console.log("no");
}
if("") {
console.log("yes");
} else {
console.log("no");
}
if("hoge") {
console.log("yes");
} else {
console.log("no");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment