Project Tools: OCaml and JavaScript (Pre Lecture)
Dr. Neil T. Dantam
CSCI-400, Colorado School of Mines
Spring 2021
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 1/23 OCaml Lineage
Lisp ML OCaml ◮ Functional ◮ Still Functional ML + some object stuff ◮ Code-is-data ◮ Less metaprogramming metaprogramming ◮ Now Statically typed ◮ Dynamically Typed
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 2/23 JavaScript c. 1995 at Netscape Communications (paraphrased)
The web must be dynamic!
Let’s put Scheme JavaScript Features in Netscape! ◮ Semantics: ◮ Largely Lisp-like: Uhh... Functional, Dynamic ◮ Just make them Some warts/weirdness think it’s Java. ◮ Syntax: C/Java-like Marc Andreeson
Brendan Eich Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 3/23 Installation
OCaml OCaml sudo apt-get install ocaml sudo apt-get install nodejs
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 4/23 Running the interpreter
OCaml JavaScript % ocaml % node # or sometimes nodejs # print string "Hello, World!" ;; > console.log("Hello, World!")
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 5/23 Emacs Tip: OCaml and JS Interpreters
OCaml JavaScript ;; Tuareg ;; js-comint M-x run-ocaml M-x js-comint-repl
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 6/23 OCaml / JS Rosetta Outline
OCaml / JS Rosetta
More OCaml
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 7/23 OCaml / JS Rosetta Function Definition
Pseudocode λa . 1+ a
OCaml JavaScript
function a −> 1+ a ( function ( a ) { return 1 + a ; } )
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 8/23 OCaml / JS Rosetta Function Application
Pseudocode (λa . 1+ a) 2
JavaScript OCaml ( function ( a ) { ( function a −> 1 + a ) 2 return 1 + a ; }) (3)
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 9/23 OCaml / JS Rosetta Conditional
Pseudocode if true then 1 else 2
JavaScript (expression)
true ? 1 : 2 OCaml
if true then 1 else 2 JavaScript (statement)
if ( true ) { console . l o g (1) } else { console . l o g (2) }
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 10/23 OCaml / JS Rosetta Local Variables
Pseudocode let a ← 2 in 1+ a
JavaScript (var)
var a = 2; console . l o g (1+a ); OCaml
let a = 2 in 1+ a JavaScript (let)
let a = 2; console . l o g (1+a );
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 11/23 OCaml / JS Rosetta JavaScript var vs. let
Var: Function Scope Let: Block Scope
( function () { ( function () { var a = 1; let a = 1; // 1st a { { var a = 2; // same a let a = 2; // 2nd a } } return a ; return a ; // 1st a }) (); }) (); // => 2 // => 1
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 12/23 OCaml / JS Rosetta Global Functions
Pseudocode defun addone (a) → 1+ a;
JavaScript (expression) OCaml function addone ( a ) { let addone a = 1+a return 1+a ; }
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 13/23 OCaml / JS Rosetta OCaml: Recursive Functions
Pseudocode OCaml
Function fact(n) let rec fact n = 1 if n = 0 then 1 if n = 0 then 1 2 else n ∗ fact(n − 1) else n ∗ fact (n−1)
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 14/23 OCaml / JS Rosetta Lists
Pseudocode
cons (1, cons (1, NIL))
OCaml (cons)
1::2::[] JavaScript (Array)
[1 ,2] OCaml (list)
[1; 2]
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 15/23 OCaml / JS Rosetta Higher-Order Functions Passing a function
Pseudocode (λf . f 2) (λa . 1+ a)
JavaScript
OCaml ( function ( f ) { return f (2); ( function f −> f 2) }) ( function ( a ) { ( function a −> 1 + a) return 1+a ; })
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 16/23 OCaml / JS Rosetta Higher-Order Functions Returning a function
Pseudocode λb . (λa . a + b)
JavaScript
OCaml ( function ( b ) { return function ( a ) { function b −> a + b ; function a −> a +b } })
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 17/23 More OCaml Outline
OCaml / JS Rosetta
More OCaml
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 18/23 More OCaml Pattern Matching Factorial
Pseudocode OCaml
Function fact(n) let rec fact n = 1 match n with match n with 2 case 0 → 1 0 −> 1 3 otherwise n ∗ fact(n − 1) | −> n ∗ fact (n−1)
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 19/23 More OCaml Pattern Matching List Copy
Pseudocode OCaml Function cpy(ℓ) 1 match ℓ with let rec cpy ell = 2 case NIL → NIL match ell with 3 case cons(a, d) → [] −> [] 4 cons(a, cpy(d)) | a::d −> a :: cpy d
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 20/23 More OCaml Unit Type
Return Type Parameter Type
let myprint s = let hello () = print string (s : string) print s t r i n g ”Hello!\ n”
(∗ myprint : string −> unit ∗) (∗ hello : unit −> unit ∗)
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 21/23 More OCaml OCaml Precedence Precedence Operator Associativity 0prefix-symbol – 1 ..(.[.{ – 2 #... left 3 func. app., constr. app., tag app., assert, lazy left 4 --. (prefix) – 5 **...lsllsrasr right 6 *.../...%...modlandlorlxor left 7 +... -... left 8 :: right 9 @... ˆ... right 10 =... <... >... |...&...$...!= left 11 &&& right 12 or || right 13 , – 14 <- := right 15 if – 16 ; right 17 let match fun function try – OCaml Manual
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 22/23 More OCaml References (you will need to read them)
OCaml Tutorial https://ocaml.org/learn/tutorials/ OCaml Reference https://ocaml.org/releases/4.11/htmlman/index.html JavaScript Spec https://www.ecma-international.org/ecma-262/10.0/index.html MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript
Dantam (Mines CSCI-400) OCaml, JavaScript (Pre Lecture) Spring 2021 23/23