mumble

A Lisp written in C, following the *Build Your Own Lisp* book
Log | Files | Refs | README

prelude.lspy (4288B)


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