Created
August 28, 2009 01:50
-
-
Save cloudhead/176740 to your computer and use it in GitHub Desktop.
learning haskell
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
doubleMe x = x + x | |
doubleUs x y = doubleMe x + doubleMe y | |
doubleSmallNumber x = if x < 100 | |
then x * 2 | |
else x | |
eoeo xs = [if mod x 2 == 0 then 'e' else 'o' | x <- xs] | |
length'' xs = sum [1 | _ <- xs] | |
combo xs ys = [x ++ " " ++ y | x <- xs, y <- ys] | |
nestedEven xxs = [[ x | x <- xs, even x] | xs <- xxs] | |
nestedEvenFlat xxs = [[x | x <- xs, even x ] | xs <- xxs] | |
maxEach xs ys = [ max (fst x) (snd x) | x <- zip xs ys] | |
maxEach' xs ys = [ max a b | (a, b) <- zip xs ys] | |
foo :: (Integral a) => a -> String | |
foo 6 = "six" | |
foo _ = "other" | |
factorial :: (Integral a) => a -> a | |
factorial 0 = 1 | |
factorial n = n * factorial (n - 1) | |
factorial' :: (Integral a) => a -> a | |
factorial' n | |
| n == 0 = 1 | |
| otherwise = n * factorial (n - 1) | |
head' :: [a] -> a | |
head' [] = error "can't head an empty list!" | |
head' (x:_) = x | |
length' :: [a] -> Int | |
length' [] = 0 | |
length' (_:xs) = 1 + length' xs | |
capital :: String -> String | |
capital "" = "" | |
capital all@(x:_) = "The first letter of " ++ all ++ " is " ++ [x] | |
bmi :: (RealFloat a) => a -> a -> String | |
bmi w h | |
| w / h ^ 2 <= 18.5 = "you're underweight" | |
| w / h ^ 2 <= 25.0 = "you're normal" | |
| w / h ^ 2 <= 30.0 = "you're fat" | |
| otherwise = "you're a whale" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment