(* * apples_oranges.sml - generic programming example * Ronald Garcia * $Id: apples_oranges.sml,v 1.2 2003/05/02 20:03:36 jewillco Exp $ *) (* Copyright 2003, Trustees of Indiana University * Please see the license in the file ../LICENSE *) (* Comparable concept *) signature Comparable = sig type value_t val better : value_t * value_t -> bool end structure Apple = struct datatype value_t = AppleT of int fun create n = AppleT n fun better ((AppleT x),(AppleT y)) = x > y end structure Orange = struct datatype value_t = OrangeT of string fun create s = OrangeT s fun better ((OrangeT x),(OrangeT y)) = x > y end (* pick - generic algorithm *) functor MakePick(C : Comparable) : sig type value_t val pick : value_t -> value_t -> value_t end = struct type value_t = C.value_t fun pick x y = if C.better(x,y) then x else y end structure ApplePick = MakePick(Apple); structure OrangePick = MakePick(Orange); val a1 = Apple.create 5; val a2 = Apple.create 6; ApplePick.pick a1 a2; val o1 = Orange.create "Miller"; val o2 = Orange.create "Portokalos"; OrangePick.pick o1 o2;