(* * Einige Definitionen aus Kapitel 1 * * Version fuer SML'97, getestet mit SML/NJ 110 (Dez. 97) *) (* Abschnitt 1.4 *) fun circleArea r = 3.1415 * r * r fun pie (r,w) = 3.1415 * r * r * w / 360.0 fun sqr (x:int) = x * x fun max (x:int,y) = if x>y then x else y fun fak n = if n=0 then 1 else n * fak (n-1) fun sum l = if l=[] then 0 else hd l + sum (tl l) fun maxl l = if l=[] then 0 else max (hd l,maxl (tl l)) fun even n = n=0 orelse n>1 andalso odd (n-1) and odd n = n=1 orelse even (n-1) ; (* Abschnitt 1.5 *) type point = real * real type polygon = point list type 'a monoid = 'a * ('a * 'a -> 'a) type ('a,'b) dict = ('a * 'b) list ; fun id x = x fun fst (x,y) = x fun ignore x = "Argument ist futsch" (* fun length l = if l=[] then 0 else 1 + length (tl l) Im folgenden wird die Version mit patern matching (s.u.) verwandt *) ; (* Abschnitt 1.6 *) datatype geo = POINT of point | CIRCLE of point * real | RECT of {lowLeft:point, upRight:point}; datatype color = RED | GREEN | BLUE; datatype tree = NODE of int * tree * tree | EMPTY (* polymorphe Version: datatype 'a tree = NODE of 'a * 'a tree * 'a tree | EMPTY *) ; fun area (POINT _) = 0.0 | area (CIRCLE (_,r)) = circleArea r | area (RECT {lowLeft=(x1,y1), upRight=(x2,y2)}) = abs ((x1-x2)*(y1-y2)) fun find (y,EMPTY) = false | find (y,NODE (x,left,right)) = if y=x then true else if yy+1) 0 l fun rev l = foldr (fn (x,y)=>y@[x]) [] l fun map f = foldr (fn (x,y)=>f x::y) [] *) fun last l = (hd o rev) l fun odd x = (not o even) x fun nestmap f = (map o map) f ; fun listConcat l = foldr op@ [] l ;