Docsity

ML Cheat Sheet, Slides of Programming Languages

Toggle sidebar
bg1
ML โ€“ Cheat sheet
๎˜๎˜‚๎˜ƒ๎˜„๎˜…๎˜„๎˜†๎˜„๎˜‡๎˜…๎˜ˆ
๎˜‰
val <name> = <newValue>;
e.g.: val x = 25;
fun <name> <param> = <expr>;
e.g.: fun addme (a, b) = a + b;
fun <name> <param> = fn <name_HOF> => <expr>;
e.g. fun g a = fn b => a + b;
let <defs> in <expr> end
e.g. let val x = 1 in x*x end
๎˜๎˜Š๎˜†๎˜Š๎˜‰๎˜†๎˜‹๎˜Œ๎˜‚๎˜ˆ
๎˜‰
char (#โ€xโ€)
string (โ€œxโ€)
int
real
bool
aโ€™ (all types)
list
๎˜๎˜Š๎˜†๎˜†๎˜‚๎˜Ž๎˜…๎˜ˆ
๎˜‰
0 => constant 0 (int)
_ => any given param matches
(a, b, c) => tuple of three elements
[a, b, c] => list of three elements
(x :: rest) => matches non-emtpy list, x is head
(x1 :: x2 :: rest) => matches list ๎˜1 elements, ...
... x1 is head, x2 element after head
fun fact 0 = 1 |
fact n = n * fact (n-1);
๎˜ ๎˜Œ๎˜‚๎˜Ž๎˜Š๎˜†๎˜‡๎˜Ž๎˜ˆ
๎˜‰
~ (negation)
+, -, *, /, div (int quotient), mod (int remainder)
^ (string concat)
>, <, <=, >= (for all numbers)
=, <> (not for reals!)
๎˜๎˜„๎˜ˆ๎˜†๎˜ˆ๎˜‰๎˜Š๎˜…๎˜‘๎˜‰๎˜†๎˜’๎˜Œ๎˜“๎˜‚๎˜ˆ
๎˜‰
List = all same type, >= 2 elements
Tuple = all types possible
() => tuple
[] => emtpy list (= nil)
[1] int list with โ€˜1โ€™
1 :: 2 :: 3 :: [] => [1, 2, 3]
hd(<list>) => first element
tl(<list>) => rest elements in list
<list1> @ <list2> => combination
๎˜‰
explode(<string>) => list of chars
implode(<list of chars> => string
real(<int>) => real
floor(<real>) => rounds down to int
ceil(<real>) => rounds up to int
round(<real>) => rounds to int
ord(<char>) => char to ASCII code
chr(<int>) => ASCII code to char
map <fun> <list> => apply <fun>...
...to each element in <list>
foldr <fun> <starter> <list> => f(x1, f(x2, (...) f(xn, c))
fol
d
l
<fun> <starter> <list> => f(xn, f(xn
-
1, (.
..) f(x1, c))
๎˜”๎˜‡๎˜…๎˜†๎˜Ž๎˜‡๎˜“๎˜‰๎˜ƒ๎˜“๎˜‡๎˜–
๎˜‰
if <expr> orelse <expr> andalso <expr>
then <expr>
else <expr>
case <expr> of
<value1> => <result1> |
<value2> => <result2> |
<value3> => <result3>;

Partial preview of the text

Download ML Cheat Sheet and more Slides Programming Languages in PDF only on Docsity!

ML โ€“ Cheat sheet

val = ; e.g.: val x = 25; fun = ; e.g.: fun addme (a, b) = a + b; fun = fn <name_HOF> => ; e.g. fun g a = fn b => a + b; let in end e.g. let val x = 1 in x*x end

char (#โ€xโ€) string (โ€œxโ€) int real bool aโ€™ (all types) list

0 => constant 0 (int) _ => any given param matches (a, b, c) => tuple of three elements [a, b, c] => list of three elements (x :: rest) => matches non-emtpy list, x is head (x1 :: x2 :: rest) => matches list 1 elements, ... ... x1 is head, x2 element after head

fun fact 0 = 1 | fact n = n * fact (n-1);

~ (negation) +, -, *, /, div (int quotient), mod (int remainder) ^ (string concat)

, <, <=, >= (for all numbers) =, <> (not for reals!)

List = all same type, >= 2 elements Tuple = all types possible () => tuple [] => emtpy list (= nil) [1] int list with โ€˜1โ€™ 1 :: 2 :: 3 :: [] => [1, 2, 3] hd () => first element tl () => rest elements in list @ => combination

explode () => list of chars implode ( => string real () => real floor () => rounds down to int ceil () => rounds up to int round () => rounds to int ord () => char to ASCII code chr () => ASCII code to char map => apply ... ...to each element in foldr => f(x1, f(x2, (...) f(xn, c)) foldl => f(xn, f(xn-1, (...) f(x1, c))

if orelse andalso then else

case of => | => | => ;