Created
April 1, 2015 12:16
-
-
Save shigemk2/b913ea0b1eab6ea0d84c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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