PROGRAM 4
(* filename: prog9.sml author: anthony f. ortiz *) (* this program converts a list to a stack and a stack to a list. *) datatype 'a stack = empty | push of ('a * 'a stack); fun list_to_stack (l: 'a list): 'a stack = if null (l) then empty else push (hd (l), list_to_stack (tl (l))); fun stack_to_list (empty) = nil | stack_to_list (push (x, y)) = [x] @ stack_to_list (y); list_to_stack ([]); list_to_stack ([1]); list_to_stack ([1, 2]); list_to_stack ([1, 2, 3]); stack_to_list (empty); stack_to_list (push (1, empty)); stack_to_list (push (1, push (2, empty))); stack_to_list (push (1, push (2, push (3, empty)))); list_to_stack ([]); list_to_stack (["cat"]); list_to_stack (["cat", "dog"]); list_to_stack (["cat", "dog", "rat"]); stack_to_list (empty); stack_to_list (push ("cat", empty)); stack_to_list (push ("cat", push ("dog", empty))); stack_to_list (push ("cat", push ("dog", push ("rat", empty)))); (* filename: prog9.out author: anthony f. ortiz *) Standard ML of New Jersey, Version 110.0.6, October 31, 1999 [CM; autoload enabl ed] - use "prog9.sml"; [opening prog9.sml] datatype 'a stack = empty | push of 'a * 'a stack val list_to_stack = fn : 'a list -> 'a stack GC #0.0.0.0.1.7: (0 ms) val stack_to_list = fn : 'a stack -> 'a list prog9.sml:15.1-15.19 Warning: type vars not generalized because of value restriction are instantiated to dummy types (X1,X2,...) val it = empty : ?.X1 stack val it = push (1,empty) : int stack val it = push (1,push (2,empty)) : int stack val it = push (1,push (2,push #)) : int stack prog9.sml:20.1-20.22 Warning: type vars not generalized because of value restriction are instantiated to dummy types (X1,X2,...) val it = [] : ?.X1 list val it = [1] : int list val it = [1,2] : int list val it = [1,2,3] : int list prog9.sml:25.1-25.19 Warning: type vars not generalized because of value restriction are instantiated to dummy types (X1,X2,...) val it = empty : ?.X1 stack val it = push ("cat",empty) : string stack val it = push ("cat",push ("dog",empty)) : string stack val it = push ("cat",push ("dog",push #)) : string stack prog9.sml:30.1-30.22 Warning: type vars not generalized because of value restriction are instantiated to dummy types (X1,X2,...) val it = [] : ?.X1 list val it = ["cat"] : string list val it = ["cat","dog"] : string list val it = ["cat","dog","rat"] : string list val it = () : unit - ^Z Stopped (user)
BACK TO CS6140 PAGE.