- Output for each of the maps
["patio","studio","ratio"] : string list
[[4],[2],[1]] : int list list
[2,7,8] : int list
["l","a","s","t"] : string list
- Defining functions using map
val ftrl = map (fn x=>3*x)
val fhel = map (hd o explode)
val fttl = map (implode o tl o tl o explode)
val fsml = map (fn x=> "s"^x^"m")
- Calculations
val totPop = reduce (op +) 0.0 (map pop europe);
val totGDP = reduce (op +) 0.0 (map gdp europe);
val popDensityMajor = let
val bigCountries = filter (fn c => pop c > 1000000.0) europe
val sum = reduce (op +) 0.0
val p = sum (map pop bigCountries)
val a = sum (map area bigCountries)
in p/a end;
(* gives number of people per square km *)
- Calculations
val big = name(hd(sort area europe));
val populous = name(hd(sort pop europe));
val wealthy = name(hd(sort gdp europe));
val rich = name(hd(sort (fn x => (gdp x)/(pop x)) europe));
Or even:
val [big, populous, wealthy, rich] = map (fn x=>name(hd(sort x europe)))
[area,pop,gdp,fn x=> gdp x/pop x];
- Determining the effect of various functions:
- r
- This is the the function rev which reverses a list
r [1,2,3] = [3,2,1]
- p
- This function is simply append with the arguments reversed.
p [1,2,3] [4,5,6] = [4,5,6,1,2,3]
- dr
- This may be used to reduce a string of decimal digits to a single
number. If the elements of the input list are not between 0 and 9 the
output is less obvious.
dr [3, 6, 2] = 263
- m
- This is the member function, m x l returns true if x is in
the list l, otherwise it returns false.
m 3 [2,6,7] = false
m 3 [2,3,6,7] = true
m 3 [3,3,3,3] = true
- n
- n x l returns true if all of the elements of l are
equal to x.
n 3 [2,6,7] = false
n 3 [2,3,6,7] = false
n 3 [3,3,3,3] = true
- im
- This is implode
im ["a", "b", "c"] = "abc"
- ts
- This is tospace it returns the elements of l upto the first
(single) space.
ts(explode "One fine day") = ["O","n","e"] : string list
val prodlist = reduce (op * ) 1
val flatten = reduce (op @) nil
val count = reduce (fn(a,b)=>1+b) 0
val duplist = reduce (fn(a,b)=>a::a::b) nil
-
- rm
- rm x l returns the list l with all occurances of x removed
- mx
- returns the maximum element of l
-
- returns the list with each element squared
- sieve
- sieve(upto 2 500) returns all prime numbers from 2 to 500.