stepeval-prelude.hs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. module Prelude where
  2. -- Arithmetic operations and some other primitives are builtin
  3. -- Type signatures are entirely useless here.
  4. -- Guards on the rhs of function equations are not supported, but in case
  5. -- expressions they are.
  6. -- combinators
  7. id x = x
  8. const x _ = x
  9. f $ x = f x
  10. -- infixr 0 $
  11. flip f x y = f y x
  12. (f . g) x = f (g x)
  13. -- infixr 9 .
  14. fix f = let x = f x in x
  15. -- booleans
  16. not True = False
  17. not False = True
  18. True || _ = True
  19. False || b = b
  20. -- infixr 2 ||
  21. False && _ = False
  22. True && b = b
  23. -- infixr 3 &&
  24. -- tuples
  25. fst (x, _) = x
  26. snd (_, x) = x
  27. curry f x y = f (x, y)
  28. uncurry f (x, y) = f x y
  29. -- lists
  30. foldr _ z [] = z
  31. foldr f z (x:xs) = x `f` foldr f z xs
  32. foldl _ acc [] = acc
  33. foldl f acc (x:xs) = foldl f (f acc x) xs
  34. -- foldl f z xs = foldr (\x r z -> r (f z x)) id xs z
  35. null [] = True
  36. null _ = False
  37. map f [] = []
  38. map f (x:xs) = f x : map f xs
  39. head (x:_) = x
  40. tail (_:xs) = xs
  41. [] ++ ys = ys
  42. (x:xs) ++ ys = x : (xs ++ ys)
  43. -- infixr 5 ++
  44. take n xs = if n <= 0 then []
  45. else case xs of
  46. [] -> []
  47. y:ys -> y : take (n - 1) ys
  48. repeat x = let xs = x : xs in xs
  49. drop n xs = if n <= 0 || null xs
  50. then xs
  51. else drop (n - 1) (tail xs)
  52. length [] = 0
  53. length (x:xs) = 1 + length xs
  54. scanl f z [] = [z]
  55. scanl f z (x:xs) = z : scanl f (f z x) xs
  56. reverse = foldl (flip (:)) []
  57. zipWith _ [] _ = []
  58. zipWith _ _ [] = []
  59. zipWith (*) (x:xs) (y:ys) = x * y : zipWith (*) xs ys
  60. iterate f x = x : iterate f (f x)