SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Part a
  2. and :: [Bool] -> Bool
  3. and []        = True
  4. and (False:_) = False
  5. and (_:xs)    = and xs
  6.  
  7. -- Part b
  8. concat :: [[a]] -> [a]
  9. concat []       = []
  10. concat (xs:xss) = xs ++ concat xss
  11.  
  12. -- Part c
  13. replicate :: Int -> a -> [a]
  14. replicate 0 _ = []
  15. replicate n a = [a] ++ replicate (n - 1) a
  16.  
  17. -- Part d
  18. -- For simplicity, assume that the index is valid
  19. -- and that the array is non-empty
  20. (!!) :: [a] -> Int -> a
  21. (x:xs) !! 0 = x
  22. (x:xs) !! n = xs !! (n - 1)
  23.  
  24. -- Part e
  25. elem :: Eq a => a -> [a] -> Bool
  26. elem _ [] = False
  27. -- Note: ys can be [] which is why (y:ys) matches singletons
  28. elem x (y:ys)
  29.     | x == y = True
  30.     | x /= y = elem x ys
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top