--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." maximum' :: (Ord a) => [a] -> a maximum' [] = error "maximum of empty list!" maximum' [x] = x maximum' (x:xs) = max x (maximum' xs) replicate' :: Int -> a -> [a] replicate' n x | n <= 0 = [] | otherwise = x : replicate' (n-1) x take' :: Int -> [a] -> [a] take' n _ | n <= 0 = [] take' _ [] = [] take' n (x:xs) = x : take' (n-1) xs reverse' :: [a] -> [a] reverse' [] = [] reverse' (x:xs) = reverse' xs ++ [x] repeat' :: a -> [a] repeat' x = x : repeat' x quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = let smallerOrEqual = [a| a<-xs, a<= x] larger = [a | a<-xs, a>x] in quicksort smallerOrEqual ++ [x] ++ quicksort larger -- Chapter 5 multThree :: Int -> Int -> Int -> Int multThree x y z = x * y * z compareWithHundred :: Int -> Ordering compareWithHundred x = compare 100 x compareWithHundred' :: Int -> Ordering compareWithHundred' = compare 100 zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c] -- 型推論をさせると zipWith' :: (t -> t1 -> a) -> [t] -> [t1] -> [a] となる zipWith' _ [] _ = [] zipWith' _ _ [] = [] zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys flip' :: (a -> b -> c) -> (b -> a -> c) flip' f = g where g x y = f y x chain :: Integer -> [Integer] chain 1 = [1] chain n | even n = n : chain (n `div` 2) | odd n = n : chain (n * 3 + 1)