Created
November 26, 2013 21:35
-
-
Save semahawk/7666680 to your computer and use it in GitHub Desktop.
This file contains some of the exercises found in "A Gentle Introduction to ML".
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
(* This file contains some of the exercises found in "A Gentle Introduction to ML" *) | |
(* right here: http://www.cs.nmsu.edu/~rth/cs/cs471/sml.html *) | |
(* Compiled using Moscow ML compiler version 2.01 *) | |
fun double x = 2 * x; | |
fun inc x = x + 1; | |
fun adda s = s ^ "a"; | |
fun triple x = 3 * x; | |
fun times4 x = double(double x); | |
fun times6 x = double(triple x); | |
fun times9 x = triple(triple x); | |
fun aveI(x, y) = (x + y) div 2; | |
fun aveR(x, y) = (x + y) / 2.0; | |
fun duplicate s = s ^ s; | |
fun quadricate s = duplicate s ^ duplicate s; | |
fun octicate s = quadricate s ^ quadricate s; | |
fun hexadecicate s = octicate s ^ octicate s; | |
fun clip s = substring(s, 0, size s - 1); | |
fun middle s = substring(s, size s div 2, 1); | |
fun dtrunc s = substring(s, 1, size s - 2); | |
fun incFirst s = implode(chr(ord(hd(explode s)) + 1)::nil) ^ substring(s, 1, size s - 1); | |
fun switch s = substring(s, size s div 2, size s div 2) ^ substring(s, 0, size s div 2); | |
fun dubmid s = substring(s, 0, size s div 2 + 1) ^ substring(s, size s div 2, size s div 2 + 1); | |
(* val fone = fn : int -> int list *) | |
fun fone(x:int) = [x,x,x]; | |
(* val 'a ftwo = fn : 'a -> 'a * 'a * 'a *) | |
fun ftwo(x) = (x,x,x); | |
(* val fthree = fn : string * string -> string list *) | |
fun fthree(x,y) = [x ^ "b", y]; | |
(* val 'a ffour = fn : int * string * 'a -> int * 'a *) | |
fun ffour(x,y,z) = (x + (size y), z) | |
val first = hd o explode; | |
val second = hd o tl o explode; | |
val third = hd o tl o tl o explode; | |
val fourth = hd o tl o tl o tl o explode; | |
(* I'm getting errors, I can't contatente a char *) | |
(*fun roll s = fourth s ^ first s ^ second s ^ third s;*) | |
(*fun exch s = second s ^ first s ^ third s ^ fourth s;*) | |
fun madeup(a, b, c) = b + size(c)::a; | |
fun factorial 0 = 1 | |
| factorial n = n * factorial(n - 1); | |
fun t 0 = 0 | |
| t n = 2 + t(n - 1); | |
fun d 0 = "de" | |
| d n = "dd" ^ d(n - 1) ^ "da"; | |
fun h 0 = 1 | |
| h n = h(n - 1) + h(n - 1); | |
fun m(a, 0) = 0 | |
| m(a, b) = a + m(a, b - 1); | |
fun f 0 = 0 | |
| f n = 1 - f(n - 1); | |
fun g 0 = 0 | |
| g n = g(n - 1) + 2 * n - 1; | |
fun l 0 = 0 | |
| l n = n mod 10 + l(n div 10); | |
fun j 0 = nil | |
| j n = (n mod 2)::j(n div 2); | |
fun sumto 0 = 0 | |
| sumto n = n + sumto(n - 1); | |
fun listfrom 0 = nil | |
| listfrom n = n::listfrom(n - 1); | |
fun strcpy(s, 0) = "" | |
| strcpy(s, n) = s ^ strcpy(s, n - 1); | |
fun power(a, 0) = 1 | |
| power(a, b) = a * power(a, b - 1); | |
fun listcpy(a, 0) = nil | |
| listcpy(a, b) = a::listcpy(a, b - 1); | |
fun sumEvens 0 = 0 | |
| sumEvens 1 = 0 | |
| sumEvens n = n + sumEvens(n - 2); | |
fun listOdds 0 = nil | |
| listOdds 1 = [1] | |
| listOdds n = n::listOdds(n - 2); | |
fun nat 0 = "zero" | |
| nat n = "succ(" ^ nat(n - 1) ^ ")"; | |
fun listTo 0 = nil | |
| listTo n = listTo(n - 1) @ [n]; | |
fun sum nil = 0 | |
| sum(h::t) = h + sum t; | |
fun doublist nil = nil | |
| doublist(h::t) = 2 * h :: doublist t; | |
fun len nil = 0 | |
| len(h::t) = 1 + len t; | |
fun triplist nil = nil | |
| triplist(h::t) = 3 * h :: triplist t; | |
fun duplist nil = nil | |
| duplist(h::t) = h::h::duplist t; | |
fun prodlist nil = 1 | |
| prodlist(h::t) = h * prodlist t; | |
fun vallist nil = nil | |
| vallist(h::t) = ord(h)::vallist t; | |
fun rev nil = nil | |
| rev(h::t) = (rev t) @ [h]; | |
fun space nil = nil | |
| space(h::t) = h::" "::space t; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment