This file contains hidden or 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
| grid :: Int -> Int -> [(Int,Int)] | |
| grid m n = [(x,y) | x <- [0..m], y <- [0..n]] | |
| square :: Int -> [(Int,Int)] | |
| square n = [(x,y) | (x,y) <- grid n n, x /= y] |
This file contains hidden or 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
| replicate :: Int -> a -> [a] | |
| replicate n x = [x | _ <- [1..n]] |
This file contains hidden or 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
| pyths :: Int -> [(Int,Int,Int)] | |
| pyths n = [(a,b,c) | c <- [1..n], | |
| b <- [1..c], | |
| a <- [1..b], | |
| a^2 + b^2 == c^2] |
This file contains hidden or 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
| factors :: Int -> [Int] | |
| factors n = [x | x <- [1..n], n `mod` x == 0] | |
| perfects :: Int -> [Int] | |
| perfects n = [x | x <- [1..n], | |
| x == sum (factors x) - ] |
This file contains hidden or 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
| list1 :: [(Int,Int)] | |
| list1 = concat [[(x,y) | y <- [3,4]] | x <- [1,2]] |
This file contains hidden or 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
| find :: Eq a => a -> [(a,b)] -> [b] | |
| find k t = [v | (k',v) <- t, k == k'] | |
| positions :: Eq a => a -> [a] -> [Int] | |
| positions x xs = [i | (x',i) <- zip xs [0..], x == x'] | |
| positions' :: Eq a => a -> [a] -> [Int] | |
| positions' x xs = find x (zip xs [0..]) |
This file contains hidden or 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
| chisqr :: [Float] -> [Float] -> Float | |
| chisqr os es = sum [((o-e)^2)/e | (o,e) <- zip os es] | |
| scalarproduct :: [Int] -> [Int] -> Int | |
| scalarproduct xs ys = sum [x*y | (x,y) <- zip xs ys] |
This file contains hidden or 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
| -- We assume that the input is not only upper case letters. | |
| import Data.Char | |
| -- Returns a list of indices of the specifed value | |
| positions :: Eq a => a -> [a] -> [Int] | |
| positions x xs = [i | (x',i) <- zip xs [0..], x == x'] | |
| -- Returns the number of lowercase characters | |
| lowers :: String -> Int | |
| lowers xs = length [x | x <- xs, 'a' <= x && x <= 'z'] |
This file contains hidden or 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
| facRecursive :: Int -> Int | |
| facRecursive n | |
| | n < 0 = 0 | |
| | n == 0 = 1 | |
| | otherwise = n * facRecursive (n - 1) |
This file contains hidden or 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
| sumdown :: Int -> Int | |
| sumdown 0 = 0 | |
| sumdown n = n + sumdown (n - 1) |