Class 20
Announcements/info Reason review, clarifications Dictionaries Variant Types Options
Class 20 Announcements/info Reason review, clarifications - - PowerPoint PPT Presentation
Class 20 Announcements/info Reason review, clarifications Dictionaries Variant Types Options Show of hands: how many are still having trouble with installing VSCode for reason? Preferred type-annotation for functions: let f : (int
Announcements/info Reason review, clarifications Dictionaries Variant Types Options
values in there too; more later)
Tank.testFull(…)
and Reason finds the relevant file, and uses the definition found there.
say
testFull(…)
so that you no longer need to use the "Tank" prefix.
let a = 4;
let (a, b) = (2, 4);
let (_, b) = (2, 4);
let [hd, ...tl] = [1, 4, 5, 6]; to get hd -> 1, tl -> [4, 5, 6];
(let ((x 5) (y 2)) (+ x y)) => 7
{ let x = 5; let y = 2; x + y }; [result: the integer 7] x; [result: Unbound value x]
/* reverse the first list, and append to it the second */ let rec revHelper: (list('a), list('a)) => list('a) = (lst, rest) => switch(lst){ | [] => rest | [hd, ...tl] => revHelper(tl, [hd, ...rest]) }; let rev: list('a) => list('a) = lst => revHelper(lst,[]);
Quiz: rewrite let rev … using a block to "hide" revHelper. You don't need to copy any of the green stuff – just write "…"
let myDict = [(“I”, “je”); (“you”, ”tu”); (“dog”, “chien”); … ] : list(string*string) better: type dict = list(string * string); let myDict = [(“I”, “je”); (“you”, ”tu”); (“dog”, “chien”); … ] :dict;
let rec lookup: (string, dict) => string = (term, dictionary) => switch(dictionary) { | [] -> ???; | [(k, v), ... tl] -> if (term == k) then v else lookup(term, tail) };
type dict = list(string * string); let myDict = [(“I”, “je”); (“you”, ”tu”); (“dog”, “chien”); … ] :dict;
let rec lookup: (string, dict) => string = (term, dictionary) => switch(dictionary) { | [] -> ???; | [(k, v), ... tl] -> if (term == k) then v else lookup(term, tail) };
isn’t in the data”, etc.
f: int => int becomes f: int => int option
let rec lookup: (string, dict) => string = (term, dictionary) => switch(dictionary) { | [] -> None; | [(k, v), ... tl] -> if (term == k) then Some(v) else lookup(term, tail) };
“Sum up all ints in a list except 1s; if empty or nothing but 1s, return None”