Added more math operations, generalized and simplified existing math operations, added some list operations for the lisp lists
This commit is contained in:
34
lib/ast.ml
34
lib/ast.ml
@@ -25,6 +25,40 @@ type lisp_val =
|
||||
recursive with lisp_val *)
|
||||
and environment = (string, lisp_val) Hashtbl.t list
|
||||
|
||||
(* It is clear that we need some primitives for working with the lisp
|
||||
data structures.
|
||||
|
||||
For example, the LCons and LNil values, together, form a linked list.
|
||||
This is the intended form of all source code in lisp, yet because
|
||||
we are using our own implementation of a linked list instead of
|
||||
ocaml's List, we can not use its many functions.
|
||||
|
||||
It may be tempting to switch to a different implementation.
|
||||
Remember however, that classic lisp semantics allow for the
|
||||
CDR component of a cons cell (the part that would point to the
|
||||
next member) to be of a type other than the list itself.
|
||||
*)
|
||||
|
||||
let reverse vs =
|
||||
let rec aux prev = function
|
||||
| LNil -> prev
|
||||
| LCons (v, next) -> aux (LCons (v, prev)) next
|
||||
| _ -> invalid_arg "cannot reverse non-list!"
|
||||
in aux LNil vs
|
||||
|
||||
let map f =
|
||||
let rec aux accum = function
|
||||
| LNil -> reverse accum
|
||||
| LCons (v, next) -> aux (LCons (f v, accum)) next
|
||||
| _ -> invalid_arg "cannot map over non-list!"
|
||||
in aux LNil
|
||||
|
||||
let reduce init f =
|
||||
let rec aux accum = function
|
||||
| LNil -> accum
|
||||
| LCons (v, next) -> aux (f accum v) next
|
||||
| _ -> invalid_arg "cannot reduce over non-list!"
|
||||
in aux init
|
||||
|
||||
let rec dbg_print_one v =
|
||||
let pf = Printf.sprintf in
|
||||
|
||||
Reference in New Issue
Block a user