参考

{% collapse title=”prelude.lspy” %}

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

{% endcollapse %}