Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. -- Ads the first two elements of a list together
  2. myFunc :: [Int] -> Int
  3. myFunc xs = (head xs) + (head $ tail xs)
  4.  
  5. myFunc
  6. |
  7. (+)
  8. /
  9. /
  10. head head
  11. |
  12. tail
  13. /
  14. xs
  15.  
  16. myFunc2 :: [Int] -> Int
  17. myFunc2 xs = hxs + (hxs `seq` (head $ tail xs))
  18. where hxs = head xs
  19.  
  20. main :: IO ()
  21. main = putStrLn "Hello, " >> putStrLn "world"
  22.  
  23. biggerThanTen :: Int -> Bool
  24. biggerThanTen n = n > 10
  25.  
  26. example :: String
  27. example = filter biggerThanTen [1..15] >> return 'a' -- This evaluates to "aaaaa"
  28.  
  29. example2 :: [Int]
  30. example2 =
  31. [1,2,3] >>=
  32. (x -> [10,100,1000] >>=
  33. (y -> return (x * y))) -- ==> [10,100,1000,20,200,2000,30,300,3000]
  34.  
  35. main :: IO ()
  36. main = do
  37. putStrLn "Hello, "
  38. putStrLn "World"
  39.  
  40. biggerThanTen :: Int -> Bool
  41. biggerThanTen n = n > 10
  42.  
  43. example :: String -- String is a synonym for [Char], by the way
  44. example = do
  45. filter biggerThanTen [1..15]
  46. return 'a'
  47.  
  48. example2 :: [Int]
  49. example2 = do
  50. x <- [1,2,3]
  51. y <- [10,100,1000]
  52. return (x * y)
  53.  
  54. do --
  55. m -- m >> n
  56. n --
  57.  
  58.  
  59. do --
  60. x <- m -- m >>= (x ->
  61. ... -- ...)
  62.  
  63. instance Monad [] where
  64. return x = [x]
  65. xs >>= f = concatMap f xs
  66.  
  67. example2 :: [Int]
  68. example2 =
  69. [1,2,3] >>=
  70. (x -> [10,100,1000] >>=
  71. (y -> return (x * y)))
  72.  
  73. example2 =
  74. concatMap
  75. (x -> [10,100,1000] >>=
  76. (y -> return (x * y)))
  77. [1,2,3]
  78.  
  79. example2 =
  80. concatMap
  81. (x -> concatMap
  82. (y -> return (x * y))
  83. [10,100,1000])
  84. [1,2,3]
  85.  
  86. example2 =
  87. concatMap
  88. (x -> concatMap
  89. (y -> [x * y])
  90. [10,100,1000])
  91. [1,2,3]
  92.  
  93. case f x of
  94. result1 -> case g y of
  95. result2 -> ....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement