(* ~dick/cs620/fun.sml Composition and others in SML *) (* Two special functions Id and \bot *) fun Id(x)=x; (*the next is one way to eemulate the idea of undefined values*) exception Undefined; fun undef(x)=raise Undefined; (* an alternative is to add an extra value to the types we are interested in... for type T, we might have datatype flat_T = Undef_T | Def of T; for example. A functor may help generate these as we need them. *) (* composition *) (*See Ullman...also find function 'o'*) (*demo*) fun foo2(F)=F o F; (* what is the type of foo2?*) fun sq(x:int)=x*x; fun cu(x:int)=sq(x)*x; fun qu(x:int)=(sq o sq) x; fun bar(x:int)=if x=1 then raise Undefined else sq(x); bar(2) handle Undefined=>42; bar(1) handle Undefined=>42; sq o cu; sq(cu(2)); (sq o cu) 2; print "----------------"; cu(2); sq(it); (sq o cu) 2; print "----------------"; foo2(sq); foo2(sq) 2; foo2(foo2); print "----------------"; sq(sq(sq(sq(2)))); foo2(foo2) sq 2; print(" You can write cond(B,F,G)!\n");