Guest User

Untitled

a guest
Aug 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import Data.List
  2. import Char
  3.  
  4. -- 5.7.1
  5.  
  6. -- > sum [x*x|x<-[1..100]]
  7. -- 338350
  8.  
  9. -- 5.7.2
  10.  
  11. replicate' :: Int -> a -> [a]
  12. replicate' x y = [y | _ <- [1..x]]
  13.  
  14. -- 5.7.3
  15.  
  16. pyths :: Int -> [(Int,Int,Int)]
  17. pyths n = [ (x,y,z) | x <- [1..n], y <- [1..n], z <- [1..n], (x * x + y * y == z * z) ]
  18.  
  19. -- 5.7.4
  20.  
  21. factors :: Int -> [Int]
  22. factors x = (\m -> nub(concat [ [x, y] | x <- [1..m], y <- [1..m], x * y == m ])) x
  23.  
  24. perfects :: Int -> [Int]
  25. perfects x = [ n | n <- [1..x], sum ( filter (\z -> z /= n) ( factors n ) ) == n ]
  26.  
  27. -- Got there. Not gracefully ;-)
  28.  
  29. -- 5.7.5
  30.  
  31. original :: [(Int,Int)]
  32. original = [ (x,y) | x <- [1,2,3], y <- [1,2,3] ]
  33.  
  34.  
  35. new :: [(Int,Int)]
  36. new = concat [ [ (x, y) | x <- [1..3]] | y <- [1..3] ]
  37.  
  38. -- 5.7.6
  39. --
  40.  
  41. positions :: Eq a => a -> [a] -> [Int]
  42. positions x xs = [ i | (x', i) <- zip xs [0..n], x == x' ]
  43. where n = length xs - 1
  44.  
  45. find' :: Eq a => a -> [(a,b)] -> [b]
  46. find' x xs = [ i | (x', i) <- xs, x == x' ]
  47.  
  48. positions' :: Eq a => a -> [a] -> [Int]
  49. positions' x xs = [ i | i <- find' x (zip xs [0..n])]
  50. where n = length xs - 1
  51.  
  52. -- 5.7.7
  53.  
  54. scaleproduct :: [Int] -> [Int] -> Int
  55. scaleproduct os es = sum [ a * b | (a, b) <- zip os es ]
  56.  
  57. -- 5.7.8
  58.  
  59. let2int :: Char -> Int
  60. let2int a = Char.ord a - Char.ord 'a'
  61.  
  62. int2let :: Int -> Char
  63. int2let a = Char.chr (a + Char.ord 'a')
  64.  
  65. shift :: Int -> Char -> Char
  66. shift n c | isLower c = int2let((let2int c + n) `mod` 26)
  67. | otherwise = c
  68.  
  69. encode :: Int -> [Char] -> [Char]
  70. encode d xs = [ shift d r | r <- xs ]
  71.  
  72. let2int' :: Char -> Int
  73. let2int a | isLower a = Char.ord a - Char.ord 'a'
  74. | isUpper a = Char.ord a - Char.ord 'A'
  75.  
  76. int2let' :: Int -> Char
  77. int2let' a | IsLower = Char.chr (a + Char.ord 'A')
  78.  
  79. shift' :: Int -> Char -> Char
  80. shift' n c | islower c = int2let((let2int c + n) `mod` 26)
  81. | isupper c = int2let((let2int c + n) `mod` 26)
  82. | otherwise = c
  83.  
  84. encode' :: Int -> [Char] -> [Char]
  85. encode' d xs = [ shift d r | r <- xs ]
Add Comment
Please, Sign In to add comment