# HG changeset patch # User Masataka Kohagura # Date 1395825232 -32400 # Node ID 8c7e1b34582fa6decc7bfa1092748aa7fe8c1144 # Parent a8d237e822a897a2d6a90bc68aa593e6079726bb add baby.hs diff -r a8d237e822a8 -r 8c7e1b34582f Haskell/baby.hs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Haskell/baby.hs Wed Mar 26 18:13:52 2014 +0900 @@ -0,0 +1,50 @@ +--http://www.haskell.org/hoogle + +{- + +-} + +doubleMe x = x + x +doubleUs x y = x * 2 + y * 2 +doubleSmallNumber x = if x > 100 + then "x" + else "y" +masa'koha' x = x + +lucky :: Int -> String +lucky 7 = "LUCKY NUMBER SEVEN" +lucky x = "Sorry, you're out of luck, pal!" + +factorial :: Int -> Int +factorial 0 = 1 +factorial n = n * factorial (n - 1) + +charName :: Char -> String +charName 'a' = "Albert" +charName 'b' = "Broseph" +charName n = "no match" + +addVectors :: (Double, Double) -> (Double, Double) -> (Double, Double) +addVectors a b = (fst a + fst b, snd a + snd b) + +head' :: [a] -> a +head' [] = error "Can't call head on an empty list,dummy!" +head' (x:_) = x + +firstLetter :: String -> String +firstLetter "" = "Empty string" +firstLetter all@(x:xs) = "The first letter of " ++ all ++ " is " ++ [x] + +bmiTell :: Double -> Double -> String +bmiTell weight height + | bmi <= 18.5 = "You're underweight, you emo, you!" + | bmi <= 25.0 = "You're supposedly normal.\\ Pffft, I bet you're ugly!" + | bmi <= 30.0 = "You're a fat," + | otherwise = "You're a whale, congratulations!" + where bmi = weight / height ^ 2 + +describeList :: [a] -> String +describeList ls = "The list is " + ++ case ls of [] -> "empty." + [x] -> "a singleton list." + xs -> "a longer list."