PageRenderTime 99ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/haskell/channel9/huttonEx5.hs

https://github.com/robtsai/mathrocks
Haskell | 96 lines | 42 code | 31 blank | 23 comment | 5 complexity | c85df9f79fbabd9dcc2fd47f27d1d59c MD5 | raw file
  1. import Data.Char
  2. -- Problem 1:
  3. -- Using list comprehension, give expression that calculates the sum of the first
  4. -- hundred integer squares
  5. sumInt100 :: Int
  6. sumInt100 = sum [x ^ 2 | x <- [1..100]]
  7. -- Problem 2:
  8. -- In a similar way to function length, show how replicate can be defined using a list comprehension
  9. replicate' :: Int -> a -> [a]
  10. replicate' n x = [x | _ <- [1..n]]
  11. -- Problem 3:
  12. -- A triple (x,y,z) of positive integers is pythagorean if x^2 + y^2 = z^2. Using a list comprehension
  13. -- define a function pyths that returns the list of all pythagroean triples whose components are at most a given limit
  14. pyths :: Int -> [(Int, Int, Int)]
  15. pyths n = [(x, y, z) | z <- [1..n], x <- [1..z], y <- [1..z], x^2 + y^2 == z^2 ]
  16. -- Problem 4:
  17. -- A positive integer is perfect if it equals the sum of its factors, excluding the number itself.
  18. -- Using a list comprehension and the function factors, define perfects that returns the list of all perfect numbers up to a given
  19. -- limit.
  20. factors :: Int -> [Int] -- returns a list of factors up to (but not including self)
  21. factors 1 = []
  22. factors n = [x | x <- [1..(n-1)], n `mod` x == 0]
  23. perfects :: Int -> [Int]
  24. perfects n = [x | x <- [1..n], x == sum (factors x)]
  25. -- Problem 5
  26. -- Show how the single comprehension [(x,y) | x <- [1,2,3], y <- [4,5,6]] with 2 generators
  27. -- can be expressed using two comprehensions with single generators
  28. -- Hint: use concat and nest one within the other
  29. p5 = [(x,y) | x <- [1,2,3], y <- [4,5,6]]
  30. p5a = concat [[(x,y) | y <- [4,5,6]] | x <- [1,2,3]]
  31. -- Problem 6
  32. -- Redefine the function positions using the function find
  33. positions :: Eq a => a -> [a] -> [Int]
  34. positions x xs = [i | (x', i) <- zip xs [0..n], x == x']
  35. where
  36. n = length xs - 1
  37. find :: Eq a => a -> [(a,b)] -> [b]
  38. find k t = [ v | (k', v) <- t, k == k']
  39. positions' :: Eq a => a -> [a] -> [Int]
  40. positions' x xs = find x (zip xs [0..(length xs - 1)])
  41. -- Problem 7
  42. -- Define a scalarproduct function
  43. -- using pattern matching and recursion, assumes list of equal length
  44. scalarproduct :: [Int] -> [Int] -> Int
  45. scalarproduct [] [] = 0
  46. scalarproduct (x:xs) (y:ys) = x * y + scalarproduct xs ys
  47. -- using a list comprehension
  48. scalarproduct' :: [Int] -> [Int] -> Int
  49. scalarproduct' xs ys = sum [ x * y | (x,y) <- zip xs ys]
  50. -- Modify the Ceasar Cipher to handle uppercaseletters
  51. let2int :: Char -> Int
  52. let2int c = ord c - ord 'a'
  53. int2let :: Int -> Char
  54. int2let n = chr (ord 'a' + n)
  55. cap2int :: Char -> Int
  56. cap2int c = ord c - ord 'A'
  57. int2cap :: Int -> Char
  58. int2cap n = chr (ord 'A' + n)
  59. shift :: Int -> Char -> Char
  60. shift n c
  61. | isLower c = int2let ((let2int c + n) `mod` 26)
  62. | isUpper c = int2cap ((cap2int c + n) `mod` 26)
  63. | otherwise = c
  64. encode :: Int -> String -> String
  65. encode n xs = [shift n x | x <- xs]