Permalink
Browse files

Added more MiniML examples

  • Loading branch information...
1 parent 19f1190 commit ba4dc02d98971f4a34b3c8fdaea7616e1d10a00c @djspiewak committed May 3, 2009
Showing with 63 additions and 0 deletions.
  1. +2 −0 examples/input/miniml/arity-clash.sml
  2. +37 −0 examples/input/miniml/example2.sml
  3. +24 −0 examples/input/miniml/example4.sml
@@ -0,0 +1,2 @@
+val x = (fn (x, y) => x) (0, 1, 2)
+
@@ -0,0 +1,37 @@
+val mapActR =
+fn f => fn (xs,state) =>
+let val iter = fn (x,(xs,state)) =>
+ let val (x,state) = f (x,state)
+ in (x :: xs, state) end
+in foldr iter (nil,state) xs end
+
+val mapActL =
+fn f => fn (xs,state) =>
+let val iter = fn (x,(xs,state)) =>
+ let val (x,state) = f (x,state)
+ in (xs @ x, state) end
+in foldl iter (nil,state) xs end
+
+val isEven = fn n => n div 2 = 0
+
+val doubleOdds =
+fn xs =>
+(* multiplies each odd element of xs by 2,
+ keeps track by how much the list sum gets incremented *)
+let val f = fn (n,inc) => if isEven n then
+ ( n, inc )
+ else
+ ( 2 * n, inc + n)
+in mapActL f (xs,0) end
+
+(* Here is a typical error report, for comparison:
+
+ex2.sml:21.1-25.24 Error: operator and operand don't agree [tycon mismatch]
+ operator domain: int * int -> 'Z list * int
+ operand: int * int -> int * int
+ in expression:
+ mapActL f
+
+*)
+
+
@@ -0,0 +1,24 @@
+val compare = fn d => fn (x,y) => x-d >= y
+
+val f =
+fn (n,m) => fn (d,e) =>
+let val square = fn x => x * x
+ val rec fac = fn x => if x = 0 then 1 else fac (x-1)
+ val x = square n
+ val y = fac (n div m)
+in if x > y then
+ compare d n m
+ else
+ compare e (n,m)
+end
+
+(* Here is a typical error report, for comparison:
+
+ex4.sml:10.9-10.22 Error: operator and operand don't agree [literal]
+ operator domain: int * int
+ operand: int
+ in expression:
+ (compare d) n
+
+*)
+

0 comments on commit ba4dc02

Please sign in to comment.