| Elements of ML Programming, 2nd Edition (ML97) |
infix 6 +;
infix 6 *;
infix 2 padd;
infix 3 pmult;
infix 4 smult;
Here is an answer to part (b), where we compute (P+2Q)*R.
([~6.0,0.0,5.0,0.0,3.0] padd 2.0 smult [4.0,~3.0,2.0,1.0]) pmult [1.0,1.0]
fun sumLeaves(x:real) = x
| sumLeaves(T1 t T2) = sumLeaves(T1) + sumLeaves(T2);
(x + abs(x))/2.0;
When x is positive, the above expression is just 2x/2 = x.
However, when x is negative, the numerator is 0.
fun addToRef(r,x) = ignore(r := !r + x);
fun isSome(NONE) = false
| isSome(SOME x) = true;
fun getOpt(NONE,x) = x
| getOpt(SOME y, _) = y;
fun ignore x = (x; ());
fun app f L = ignore(map f L);
Notice the difference:
app print ["ab", "cd"];
abcdval it = () : unit
map print ["ab", "cd"];
abcdval it = [(),()] : unit list
fun compare(nil,nil) = EQUAL
| compare(nil,_) = LESS
| compare(_,nil) = GREATER
| compare(x::xs,y::ys) =
if x<y then LESS
else if x>y then GREATER
else compare(xs,ys);
Notice that the function compare works only on integer lists,
because integer is the default type for <.
x * sign(x)
When x is positive, sign(x) is 1, so the result is
x.
When x is negative, sign(x) is -1, so the result is
-x, i.e., abs(x).
When x is 0, then sign(x) is also 0, and the expression
is surely 0.
~1073741821
0wx1F
0wx4
fun isVowel c = Char.contains "aeiou" c;
val f = String.tokens(fn x => x = #" " orelse x = #"\n" orelse x = #"\t");
fun rev1 ss =
case Substring.getc ss of
NONE => "" |
SOME (c,ss1) => rev1 ss1 ^ str c;
fun revString s = rev1(Substring.substring(s,0,size s));
The work is done by the function rev1, which uses getc
to break a substring into a first character and a subsequent
characters.
If the substring is empty, we return the empty string (not substring).
If the string has a first character, we recursively reverse the
substring consisting of the remaining characters, receiving a string as
the result.
Finally, we concatenate the first character to the end of the result
string.Then, since we are given a string initially, we need function revString to convert the string to an equivalent substring and call rev1 on this substring. Notice that we get the complete string as a substring by taking the second argument of Substring.substring to be 0 and the third argument to be the length of the string.
[0,1,4,9]
fun threeWay(L) =
let
val (L0,M) = List.partition (fn x => x mod 3 = 0) L;
val (L1,L2) = List.partition (fn x => x mod 3 = 1) M
in
(L0,L1,L2)
end;
Here, L0, L1, and L2 are the lists of integers that have remainders 0,
1, and 2, respectively, when divided by 3.
Download this program
fun chArray() = Array.tabulate(26, (fn x => chr(x+ord #"a")));
#[1,4,9,16,25]
TIME {sec=1, usec=234000}
fun sum1 nil = 0
| sum1(x::xs) = valOf(Int.fromString x) + sum1 x;
Then, the following function
calls sum1
on its second argument, ignoring the first, and prints the result.
fun sum(_,L) = print(Int.toString(sum1 L));
The first argument is there because of the form of function
exportFn expects; it is the name of the file containing the
program being exported.Finally, we export the function sum into a file foo:
exportFn("foo",sum);