|
|
fun map _ [ ] = [ ] | map f (h::t) = (f h)::(map f t); |
fun filter _ [ ] = [ ]
| filter p (h::t) =
if (p h) then h::(filter p t)
else filter p t;
|
fun reduce _ a [ ] = a | reduce F a (h::t) = F h (reduce F a t); |
fun map2 _ [ ] [ ] = [ ] | map2 F (h1::t1) (h2::t2) = (F h1 h2) :: (map2 F t1 t2); |
| datatype IS = I of int | S of string; | fun m (I x) (I y) = I (x *
y) | m (I x) (S y) = (S y); |
| fun g x y = (x, y); | fun h x y = y@[x]; |
g 2 "b"; ___(2,"b")_____
h [3, 2] 1 ____Fails ____
map (fn a => a + 1) [1,2,3,4]; ___[2,3,4,5] ___
map2 g [1,2] ["a","b"]; _[(1,"a"), (2,"b")]
reduce h [ ] [1,2,3]; ___[3,2,1] _____
filter (fn x => x > 3) [1,2,3,4,5]; ___[4,5] _____
m (I 5) (S "a"); _____S "a"______
m (I 5) (I 6); _____I 30_______
m (S "a") (I 5); _____Fails _____
Domain of g __polymorphic___
Range of g _polymorphic tuples_
Define the function
count5 to count the number of 5's in a list, for example:
count5 [] is 0
count5 [1,5,2,5,3,5] is 3
fun count5 [] = 0
| count5 (h::t) = if h=5 then 1+count5 t
else count5 t;
Given a union data type
definition of:
datatype IRS = I of int | R of real | S of string | IR of (int
* real) | IS of (int * string);
Define the Imult function that multiples two I IRS types and returns an I IRS
type. Ignore the others. For example:
Imult (I 5) (I 4); is (I 20)
fun Imult (I x) (I y) = I(x*y);
Using the datatype
definition of Question 26, define the IxR function that given a I IRS type and
a R IRS type returns an IR IRS type. For example:
IxR (I 4) (R 5.2); returns (IR (4,5.2) )
fun IxR (I i) (R r) = (IR (i, r));
Given the datatype
definition of:
datatype 'element mylist = NIL | CONS of 'element * 'element
mylist;
Define the function to count the number of 'elements. For example:
count CONS(8,CONS(2,CONS(6, NIL))); is 3
fun count NIL = 0
| count (CONS(h,t)) = 1 + count t;