(**** ML Programs from the book ML for the Working Programmer by Lawrence C. Paulson, Computer Laboratory, University of Cambridge. (Cambridge University Press, 1991) Copyright (C) 1991 by Cambridge University Press. Permission to copy without fee is granted provided that this copyright notice and the DISCLAIMER OF WARRANTY is included in any copy. DISCLAIMER OF WARRANTY. These programs are provided `as is' without warranty of any kind. We make no warranties, express or implied, that the programs are free of error, or are consistent with any particular standard of merchantability, or that they will meet your requirements for any particular application. They should not be relied upon for solving a problem whose incorrect solution could result in injury to a person or loss of property. If you do use the programs or functions in such a manner, it is at your own risk. The author and publisher disclaim all liability for direct, incidental or consequential damages resulting from your use of these programs or functions. ****) (*** Basic library module. From Chapter 9. ***) infix mem; functor BasicFUN() : BASIC = struct fun length [] = 0 | length (_::t) = 1 + length t fun minl[m] : int = m | minl(m::n::ns) = if mn then maxl(m::ns) else maxl(n::ns); fun take (n, []) = [] | take (n, x::xs) = if n>0 then x::take(n-1,xs) else []; fun drop (_, []) = [] | drop (n, x::xs) = if n>0 then drop (n-1, xs) else x::xs; exception Nth; fun nth (l,n) = (*numbers the list elements [x0,x1,x2,...] *) case drop(n,l) of [] => raise Nth | x::_ => x; fun x mem [] = false | x mem (y::l) = (x=y) orelse (x mem l); (*insertion into list if not already there*) fun newmem(x,xs) = if x mem xs then xs else x::xs; exception Lookup; fun lookup ([], a) = raise Lookup | lookup ((x,y)::pairs, a) = if a=x then y else lookup(pairs, a); fun filter pred [] = [] | filter pred (x::xs) = if pred(x) then x :: filter pred xs else filter pred xs; fun exists pred [] = false | exists pred (x::xs) = (pred x) orelse exists pred xs; fun forall pred [] = true | forall pred (x::xs) = (pred x) andalso forall pred xs; fun foldleft f (e, []) = e | foldleft f (e, x::xs) = foldleft f (f(e,x), xs); fun foldright f ([], e) = e | foldright f (x::xs, e) = f(x, foldright f (xs,e)); end;