0% found this document useful (0 votes)
164 views

CS312 SML / NJ Cheat Sheet

This document contains a cheat sheet for the Standard ML (SML) programming language and the New Jersey (NJ) implementation. It provides summaries of basic SML syntax like data types, functions, pattern matching, and lists as well as functions for manipulating lists like map, fold, filter. It also includes examples of defining and using algebraic data types, references, exceptions, and modules with signatures.

Uploaded by

PrasunGhosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views

CS312 SML / NJ Cheat Sheet

This document contains a cheat sheet for the Standard ML (SML) programming language and the New Jersey (NJ) implementation. It provides summaries of basic SML syntax like data types, functions, pattern matching, and lists as well as functions for manipulating lists like map, fold, filter. It also includes examples of defining and using algebraic data types, references, exceptions, and modules with signatures.

Uploaded by

PrasunGhosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

CS312 SML / NJ Cheat Sheet

() : unit
3 : int List.filter : ('a -> bool) -> 'a list -> 'a list
3.0 : real List.filter (fn x => x < 4) [2,4,3,9,6,1,0,5] [2,3,1,0]
#"a" : char
"xyz" : string List.foldr : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
false : bool List.foldr (op ^) "x" ["a","b","c"] "abcx"
3 < 5 andalso true : bool
SOME 3 : int option List.foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
NONE : 'a option List.foldl (op ^) "x" ["a","b","c"] "cbax"
ref 3 : int ref
[3,4] : int list List.find : ('a -> bool) -> 'a list -> 'a option
[] : 'a list List.find (fn x => x > 10) [1,5,10,13,19] SOME 13
(1,"xyz",3.0) : int * string * real
{foo=6,bar="xyz"} : {foo:int,bar:string} size "hello" 5
fn x => x + 1 : int -> int length [8,9,10] 3
fn x y => x + y : int -> int -> int rev [8,9,10] [10,9,8]
fn (x,y) => x + y : int * int -> int valOf (SOME 312) 312
fn () => 4 : unit -> int isSome (SOME 312) true

if x < 0 orelse x > 0 let val (x,y) = (SOME 12,300)


then "nonzero" in case (x,y) of
else "zero" (SOME z,_) => z + y
| (NONE,_) => y
case x of end 312
0 => "zero"
| 1 => "one" let
| _ => "more than one" val (e,pi) = (Math.e,Math.pi)
fun f x y = {e=x,pi=y}
Char.ord #"a" 97 in f e pi
Char.ord #"A" 65 end {e=2.71828182846,pi=3.14159265359}
Char.ord #"0" 48
Char.chr 97 #"a" let
explode "abc" [#"a",#"b",#"c"] fun uncurried(x,y) = x + y
implode [#"a",#"b",#"c"] "abc" fun curried x y = x + y
in (uncurried(1,2), curried 1 2)
(fn x => x + 1) 3 4 end (3,3)
"x" ^ "y" ^ "z" "xyz"
~5 + 7 2 fun sum(x:int list):int =
case x of [] => 0
let | u::t => u + sum t
fun f x = x * x
val ff = f o f signature STACK = sig
val fff = f o f o f type 'a stack
in exception Empty of string
(f 2,ff 2,fff 2) val new : unit -> 'a stack
end (4,16,256) val push : 'a stack * 'a -> 'a stack
val pop : 'a stack -> 'a * 'a stack
hd [3,4] 3 val isEmpty : 'a stack -> bool
tl [3,4] [4] end
tl [4] [] = nil
3::[4,5] [3,4,5] structure Stack : STACK = struct
[1,2,3] @ [4,5,6] [1,2,3,4,5,6] type 'a stack = 'a list
null [] true exception Empty of string
null [3,4] false fun new() = nil
fun push(s,x) = x::s
#2 (1,"abc",3.4) "abc" fun pop s =
#foo {foo=6,bar="xyz"} 6 case s of x::t => (x,t)
| [] => raise Empty "empty"
datatype 'a option = SOME of 'a | NONE val isEmpty = List.null
datatype order = LESS | EQUAL | GREATER end
datatype 'a stack = EMPTY | TOP of ('a * 'a stack)
let val xr:int ref = ref 299
map : ('a -> 'b) -> 'a list -> 'b list in xr := !xr + 13
map (fn x => x + 100) [2,3,4] [102,103,104] end () sets xr to 312 as a side effect
map (fn x => x = 3) [2,3,4] [false,true,false]
(print "hello"; 312) 312
List.tabulate : int * (int -> 'a) -> 'a list prints "hello" as a side effect
List.tabulate (4,fn x => x*x) [0,1,4,9]

You might also like