Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Auto-loading the std lists library now. Added a bunch of tests to the…

… test script.
  • Loading branch information...
commit f4037d79a0922c1b3bcd320c91c43c8590fab19b 1 parent 7f924e2
@massung authored
Showing with 43 additions and 2 deletions.
  1. +11 −0 core.ml
  2. +1 −0  main.ml
  3. +10 −1 pkg/std/lists.fc
  4. +21 −1 test.fc
View
11 core.ml
@@ -63,6 +63,16 @@ let prim_str = function
end
| _ -> raise Arity_mismatch
+let prim_len = function
+ | [xs] ->
+ begin
+ match xs with
+ | List xs -> Num (Fixnum.Int (List.length xs))
+ | String s -> Num (Fixnum.Int (String.length s))
+ | x -> raise (Type_error x)
+ end
+ | _ -> raise Arity_mismatch
+
(* core package that all packages get *)
let prims =
[ ("chan", prim_chan)
@@ -73,4 +83,5 @@ let prims =
; ("tl", prim_tl)
; ("puts", prim_puts)
; ("str", prim_str)
+ ; ("len", prim_len)
]
View
1  main.ml
@@ -53,5 +53,6 @@ let _ =
(*Repl.setup_term ();*)
let st = Interp.new_thread 20000 in
let word s x = Printf.printf "%s: %s\n" s (Cell.show (!x)) in
+ let lists = file_in st "pkg/std/lists.fc" in
let pkg = file_in st "test.fc" in
Hashtbl.iter word pkg
View
11 pkg/std/lists.fc
@@ -39,5 +39,14 @@ let combine xs ys = for x,y in xs,ys do (x,y)
;; select elements from a list that match a predicate
let filter pred xs = for x in xs when pred(x) do x
+;; divide a list into a pair of two lists
+;let split xs
+; | xs == [] = ([],[])
+; | tl(xs) == [] = (xs,[])
+; | otherwise =
+; let y = hd(tl(xs)) in
+; let p = split(tl(tl(xs))) in
+; ([hd(xs) | fst(p)], [y | snd(p)])
+
;; randomly order the elements of a list
-let shuffle xs = let rs = for x in xs do (rand(),x) in map(snd,sort(rs))
+;let shuffle xs = let rs = for x in xs do (rand(),x) in map(snd,sort(rs))
View
22 test.fc
@@ -2,6 +2,9 @@
package main
+;; import a function
+from lists import combine
+
;; list comprehensions
let pips = [2,3,4,5,6,7,8,9,10,11,12,13,14]
let suits = [Clubs,Diamonds,Hearts,Spades]
@@ -59,4 +62,21 @@ let aux =
z(5)
;; using a created range
-let matrix = for x,y in 1 to 3,1 to 3 do (x*y)
+let m_rng = 1 to 3
+let matrix = for x,y in m_rng,m_rng do (x*y)
+
+;; native function call test
+let len_of_xs = len(suits)
+let len_of_name = len(jeff.name.first)
+
+;; use another module
+let odds = let odd n = n % 2 /= 0 in lists:filter(odd,lists:iota(10))
+let evens = let even n = n % 2 == 0 in lists:filter(even,lists:iota(10))
+
+;; use an imported function
+let numbers = combine(odds,evens)
+
+;; consing lists
+let cons x xs = [x,A,B,C|xs]
+let my_cons = cons(1,[2,3,4])
+
Please sign in to comment.
Something went wrong with that request. Please try again.