This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun rev1(nil, M) = M | |
| | rev1(x::xs, ys) = rev1(xs, x::ys); | |
| fun reverse(L) = rev1(L, nil); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun nth(L, n) = | |
| if n = 1 then hd(L) | |
| else nth(tl(L), n-1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun padd(P, nil) = P | |
| | padd(nil, Q) = Q | |
| | padd((p:real)::ps, q::qs) = (p+q)::padd(ps, qs); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun reverse(nil) = nil | |
| | reverse(x::xs) = reverse(xs)@[x]; | |
| fun palindrome(x) = | |
| let | |
| val xchars = explode(x) | |
| in | |
| xchars = reverse(xchars) | |
| end; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun partition(p, nil) = (nil, nil) | |
| | partition(p, x::xs) = | |
| let | |
| val (ys, zs) = partition(p, xs) | |
| in | |
| if p(x) then (x::ys, zs) | |
| else (ys, x::zs) | |
| end; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use "interleave.sml"; | |
| fun perm nil = [nil] | |
| | perm (x::xs) = List.concat (map (interleave x) (perm xs)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val rec padd = fn | |
| (P, nil) => P | |
| | (nil, Q) => Q | |
| | ((p:real)::ps, q::qs) => (p+q)::padd(ps, qs); | |
| val rec smult = fn | |
| (nil, q) => nil | |
| | ((p:real)::ps, q) => (p*q)::smult(ps, q); | |
| val rec pmult = fn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun padd(P, nil) = P | |
| | padd(nil, Q) = Q | |
| | padd((p:real)::ps, q::qs) = (p+q)::padd(ps, qs); | |
| fun smult(nil, q) = nil | |
| | smult((p:real)::ps, q) = (p*q)::smult(ps, q); | |
| fun pmult(P, nil) = nil | |
| | pmult(P, q::qs) = padd(smult(P, q), 0.0::pmult(P, qs)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun power(x, 0) = 1 | |
| | power(x, i) = x*power(x, i-1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun consset(a, nil) = nil | |
| | consset(a, x::xs) = (a::x)::consset(a, xs); | |
| fun powerset(nil) = [nil] | |
| | powerset(x::xs) = | |
| let | |
| val p = powerset(xs) | |
| in | |
| p@consset(x, p) | |
| end; |