standard-definitions.hs 965 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. map :: (a -> b) -> [a] -> [b]
  2. map f [] = []
  3. map f (x:xs) = f x : map f xs
  4. ----------
  5. zipWith :: (a->b->c) -> [a]->[b]->[c]
  6. zipWith z (a:as) (b:bs)
  7. = z a b : zipWith z as bs
  8. zipWith _ _ _ = []
  9. ----------
  10. zip :: [a] -> [b] -> [(a,b)]
  11. zip = zipWith (,)
  12. ----------
  13. unzip :: [(a,b)] -> ([a],[b])
  14. unzip = foldr (\(a,b) ~(as,bs) -> (a:as,b:bs)) ([],[])
  15. ----------
  16. foldl :: (a -> b -> a) -> a -> [b] -> a
  17. foldl f z [] = z
  18. foldl f z (x:xs) = foldl f (f z x) xs
  19. ----------
  20. foldr :: (a -> b -> b) -> b -> [a] -> b
  21. foldr f z [] = z
  22. foldr f z (x:xs) = f x (foldr f z xs)
  23. ----------
  24. take :: Int -> [a] -> [a]
  25. take n _ | n <= 0 = []
  26. take _ [] = []
  27. take n (x:xs) = x : take (n-1) xs
  28. ----------
  29. splitAt :: Int -> [a] -> ([a],[a])
  30. splitAt n xs = (take n xs, drop n xs)
  31. ----------