import Control.Monad -- factorial x = product [1..x] factorial 0 = 1 factorial n = n * factorial (n-1) -- choose n k = if n < 0 then error "n should be positive" else factorial n `div` factorial k `div` factorial (n-k) choose n k | n < 0 = error "n should be nonnegative" | k < 0 = error "k should be nonnegative" | k > n = error "k should be <= n" | otherwise = factorial n `div` factorial k `div` factorial (n-k) foo = 12345 fibo 0 = 0 fibo 1 = 1 fibo n = fibo (n - 1) + fibo (n - 2) -- i = 0, 1, 2, 3, 4, 5, 6, 7 , 8, ... -- fibo i = 0, 1, 1, 2, 3, 5, 8, 13, 21, ... -- (0, 1) -> (1, 1) -> ( -- fibo2 k = (kth fibo number, (k+1)th fibo number) fibo2 0 = (0, 1) fibo2 n = let (x, y) = fibo2 (n-1) in (y, x + y) -- if n = 8 -- then x = 13 -- and y = 21 fiboList = 0 : 1 : zipWith (+) fiboList (tail fiboList) mymap f [] = [] mymap f (x:xs) = f x : mymap f xs spliceOne [] = [] spliceOne (x:xs) = (x, xs) : [(y, x:ys) | (y, ys) <- spliceOne xs] permutations [] = [[]] permutations xs = [y : p | (y, ys) <- spliceOne xs, p <- permutations ys] -- rollFiveDice = [[d1, d2, d3, d4, d5] | d1 <- [1..6], d2 <- [1..6], d3 <- [1..6], d4 <- [1..6], d5 <- [1..6]] rollFiveDice = replicateM 5 [1..6]