Reference


prelude.lspy

  1. ;;;
  2. ;;; Lispy Standard Prelude
  3. ;;;
  4. ;;; Atoms
  5. (def {nil} {})
  6. (def {true} 1)
  7. (def {false} 0)
  8. ;;; Functional Functions
  9. ; Function Definitions
  10. (def {fun} (\ {f b} {
  11. def (head f) (\ (tail f) b)
  12. }))
  13. ; Open new scope
  14. (fun {let b} {
  15. ((\ {_} b) ())
  16. })
  17. ; Unpack List to Function
  18. (fun {unpack f l} {
  19. eval (join (list f) l)
  20. })
  21. ; Unapply List to Function
  22. (fun {pack f & xs} {f xs})
  23. ; Curried and Uncurried calling
  24. (def {curry} unpack)
  25. (def {uncurry} pack)
  26. ; Perform Several things in Sequence
  27. (fun {do & l} {
  28. if (== l nil)
  29. {nil}
  30. {last l}
  31. })
  32. ;;; Logical Functions
  33. ; Logical Functions
  34. (fun {not x} {- 1 x})
  35. (fun {or x y} {+ x y})
  36. (fun {and x y} {* x y})
  37. ;;; Numeric Functions
  38. ; Minimum of Arguments
  39. (fun {min & xs} {
  40. if (== (tail xs) nil) {fst xs}
  41. {do
  42. (= {rest} (unpack min (tail xs)))
  43. (= {item} (fst xs))
  44. (if (< item rest) {item} {rest})
  45. }
  46. })
  47. ; Maximum of Arguments
  48. (fun {max & xs} {
  49. if (== (tail xs) nil) {fst xs}
  50. {do
  51. (= {rest} (unpack max (tail xs)))
  52. (= {item} (fst xs))
  53. (if (> item rest) {item} {rest})
  54. }
  55. })
  56. ;;; Conditional Functions
  57. (fun {select & cs} {
  58. if (== cs nil)
  59. {error "No Selection Found"}
  60. {if (fst (fst cs)) {snd (fst cs)} {unpack select (tail cs)}}
  61. })
  62. (fun {case x & cs} {
  63. if (== cs nil)
  64. {error "No Case Found"}
  65. {if (== x (fst (fst cs))) {snd (fst cs)} {
  66. unpack case (join (list x) (tail cs))}}
  67. })
  68. (def {otherwise} true)
  69. ;;; Misc Functions
  70. (fun {flip f a b} {f b a})
  71. (fun {ghost & xs} {eval xs})
  72. (fun {comp f g x} {f (g x)})
  73. ;;; List Functions
  74. ; First, Second, or Third Item in List
  75. (fun {fst l} { eval (head l) })
  76. (fun {snd l} { eval (head (tail l)) })
  77. (fun {trd l} { eval (head (tail (tail l))) })
  78. ; List Length
  79. (fun {len l} {
  80. if (== l nil)
  81. {0}
  82. {+ 1 (len (tail l))}
  83. })
  84. ; Nth item in List
  85. (fun {nth n l} {
  86. if (== n 0)
  87. {fst l}
  88. {nth (- n 1) (tail l)}
  89. })
  90. ; Last item in List
  91. (fun {last l} {nth (- (len l) 1) l})
  92. ; Apply Function to List
  93. (fun {map f l} {
  94. if (== l nil)
  95. {nil}
  96. {join (list (f (fst l))) (map f (tail l))}
  97. })
  98. ; Apply Filter to List
  99. (fun {filter f l} {
  100. if (== l nil)
  101. {nil}
  102. {join (if (f (fst l)) {head l} {nil}) (filter f (tail l))}
  103. })
  104. ; Return all of list but last element
  105. (fun {init l} {
  106. if (== (tail l) nil)
  107. {nil}
  108. {join (head l) (init (tail l))}
  109. })
  110. ; Reverse List
  111. (fun {reverse l} {
  112. if (== l nil)
  113. {nil}
  114. {join (reverse (tail l)) (head l)}
  115. })
  116. ; Fold Left
  117. (fun {foldl f z l} {
  118. if (== l nil)
  119. {z}
  120. {foldl f (f z (fst l)) (tail l)}
  121. })
  122. ; Fold Right
  123. (fun {foldr f z l} {
  124. if (== l nil)
  125. {z}
  126. {f (fst l) (foldr f z (tail l))}
  127. })
  128. (fun {sum l} {foldl + 0 l})
  129. (fun {product l} {foldl * 1 l})
  130. ; Take N items
  131. (fun {take n l} {
  132. if (== n 0)
  133. {nil}
  134. {join (head l) (take (- n 1) (tail l))}
  135. })
  136. ; Drop N items
  137. (fun {drop n l} {
  138. if (== n 0)
  139. {l}
  140. {drop (- n 1) (tail l)}
  141. })
  142. ; Split at N
  143. (fun {split n l} {list (take n l) (drop n l)})
  144. ; Take While
  145. (fun {take-while f l} {
  146. if (not (unpack f (head l)))
  147. {nil}
  148. {join (head l) (take-while f (tail l))}
  149. })
  150. ; Drop While
  151. (fun {drop-while f l} {
  152. if (not (unpack f (head l)))
  153. {l}
  154. {drop-while f (tail l)}
  155. })
  156. ; Element of List
  157. (fun {elem x l} {
  158. if (== l nil)
  159. {false}
  160. {if (== x (fst l)) {true} {elem x (tail l)}}
  161. })
  162. ; Find element in list of pairs
  163. (fun {lookup x l} {
  164. if (== l nil)
  165. {error "No Element Found"}
  166. {do
  167. (= {key} (fst (fst l)))
  168. (= {val} (snd (fst l)))
  169. (if (== key x) {val} {lookup x (tail l)})
  170. }
  171. })
  172. ; Zip two lists together into a list of pairs
  173. (fun {zip x y} {
  174. if (or (== x nil) (== y nil))
  175. {nil}
  176. {join (list (join (head x) (head y))) (zip (tail x) (tail y))}
  177. })
  178. ; Unzip a list of pairs into two lists
  179. (fun {unzip l} {
  180. if (== l nil)
  181. {{nil nil}}
  182. {do
  183. (= {x} (fst l))
  184. (= {xs} (unzip (tail l)))
  185. (list (join (head x) (fst xs)) (join (tail x) (snd xs)))
  186. }
  187. })
  188. ;;; Other Fun
  189. ; Fibonacci
  190. (fun {fib n} {
  191. select
  192. { (== n 0) 0 }
  193. { (== n 1) 1 }
  194. { otherwise (+ (fib (- n 1)) (fib (- n 2))) }
  195. })