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
datatype ('label) tree = | |
Node of 'label * 'label tree list; | |
fun sum(Node(a, nil)) = a | |
| sum(Node(a, t::ts)) = sum(t) + sum(Node(a, ts)); |
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 swap nil = nil | |
| swap [x] = [x] | |
| swap (x::y::zs) = y::x::(swap zs); |
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 swapNeighbor(nil) = nil | |
| swapNeighbor(x::nil) = x::nil | |
| swapNeighbor(x::y::zs) = y::x::swapNeighbor(zs); |
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 testZero(0) = print("zero\n") | |
| testZero(_) = print("not zero\n"); |
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 trap(a, b, n, F) = | |
if n <= 0 orelse b-a <= 0.0 then 0.0 | |
else | |
let | |
val delta = (b-a)/real(n) | |
in | |
delta * (F(a) + F(a+delta))/2.0 + | |
trap(a+delta, b, n-1, F) | |
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 square(x:real) = x*x; | |
fun plus(x:real, y) = x+y; | |
fun length(nil) = 0.0 | |
| length(x::xs) = 1.0 + length(xs); | |
fun variance(L) = | |
let | |
val n = length(L) | |
in | |
reduce(plus, map(square, L))/n - | |
square(reduce(plus, L)/n) |
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 sqrt = Math.sqrt; | |
fun vec_length(nil) = nil | |
| vec_length((x, y)::zs) = sqrt(x * x + y * y)::vec_length(zs); |
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 sqrt = Math.sqrt; | |
fun simpleMap(f, nil) = nil | |
| simpleMap(f, x::xs) = f(x) :: simpleMap(f, xs); | |
fun vec_length(L) = simpleMap(fn (x, y) => sqrt(x * x + y * y), L); | |
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
(define factorial | |
(lambda (n) | |
(if (= n 0) | |
1 | |
(* n (factorial (- 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 remove_a_stone vec i = | |
let | |
open VectorSlice | |
val size = Vector.length vec - 1 | |
val newvec = | |
if i = 0 then vector (slice (vec, 1, SOME size)) | |
else if i = size then vector (slice (vec, 0, SOME size)) | |
else concat [slice (vec, 0, SOME i), | |
slice (vec, i+1, SOME (size-i))] | |
in |