/haskell/channel9/huttonEx5.hs
Haskell | 96 lines | 42 code | 31 blank | 23 comment | 5 complexity | c85df9f79fbabd9dcc2fd47f27d1d59c MD5 | raw file
- import Data.Char
- -- Problem 1:
- -- Using list comprehension, give expression that calculates the sum of the first
- -- hundred integer squares
- sumInt100 :: Int
- sumInt100 = sum [x ^ 2 | x <- [1..100]]
- -- Problem 2:
- -- In a similar way to function length, show how replicate can be defined using a list comprehension
- replicate' :: Int -> a -> [a]
- replicate' n x = [x | _ <- [1..n]]
- -- Problem 3:
- -- A triple (x,y,z) of positive integers is pythagorean if x^2 + y^2 = z^2. Using a list comprehension
- -- define a function pyths that returns the list of all pythagroean triples whose components are at most a given limit
- pyths :: Int -> [(Int, Int, Int)]
- pyths n = [(x, y, z) | z <- [1..n], x <- [1..z], y <- [1..z], x^2 + y^2 == z^2 ]
- -- Problem 4:
- -- A positive integer is perfect if it equals the sum of its factors, excluding the number itself.
- -- Using a list comprehension and the function factors, define perfects that returns the list of all perfect numbers up to a given
- -- limit.
- factors :: Int -> [Int] -- returns a list of factors up to (but not including self)
- factors 1 = []
- factors n = [x | x <- [1..(n-1)], n `mod` x == 0]
- perfects :: Int -> [Int]
- perfects n = [x | x <- [1..n], x == sum (factors x)]
- -- Problem 5
- -- Show how the single comprehension [(x,y) | x <- [1,2,3], y <- [4,5,6]] with 2 generators
- -- can be expressed using two comprehensions with single generators
- -- Hint: use concat and nest one within the other
- p5 = [(x,y) | x <- [1,2,3], y <- [4,5,6]]
- p5a = concat [[(x,y) | y <- [4,5,6]] | x <- [1,2,3]]
- -- Problem 6
- -- Redefine the function positions using the function find
- positions :: Eq a => a -> [a] -> [Int]
- positions x xs = [i | (x', i) <- zip xs [0..n], x == x']
- where
- n = length xs - 1
- 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 = find x (zip xs [0..(length xs - 1)])
- -- Problem 7
- -- Define a scalarproduct function
- -- using pattern matching and recursion, assumes list of equal length
- scalarproduct :: [Int] -> [Int] -> Int
- scalarproduct [] [] = 0
- scalarproduct (x:xs) (y:ys) = x * y + scalarproduct xs ys
- -- using a list comprehension
- scalarproduct' :: [Int] -> [Int] -> Int
- scalarproduct' xs ys = sum [ x * y | (x,y) <- zip xs ys]
- -- Modify the Ceasar Cipher to handle uppercaseletters
- let2int :: Char -> Int
- let2int c = ord c - ord 'a'
- int2let :: Int -> Char
- int2let n = chr (ord 'a' + n)
- cap2int :: Char -> Int
- cap2int c = ord c - ord 'A'
- int2cap :: Int -> Char
- int2cap n = chr (ord 'A' + n)
- shift :: Int -> Char -> Char
- shift n c
- | isLower c = int2let ((let2int c + n) `mod` 26)
- | isUpper c = int2cap ((cap2int c + n) `mod` 26)
- | otherwise = c
- encode :: Int -> String -> String
- encode n xs = [shift n x | x <- xs]