Last active
October 4, 2016 05:47
-
-
Save abhijith/3091302 to your computer and use it in GitHub Desktop.
OCaml baby steps
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
(* random code samples *) | |
Char.code 'a' (* => 97 *) | |
Char.uppercase 'a' | |
Char.chr 33 (* => ! *) | |
(* STRING CONCATENATION *) | |
"hello" ^ "world" | |
"hello".[1] | |
String.length "hello" | |
String.sub "asdf" 1 2 (* substring => sd *) | |
if true then 1 else 2 | |
let x = 1 | |
let y = 2 | |
let z = x + y | |
(* OR *) | |
(* Sequential definition *) | |
let x = 100;; | |
let z = | |
let x = 1 in | |
let y = 2 in | |
x + y;; | |
(* Simultaneous definition *) | |
let x = 1 and y = x in y + x;; (* 100 + 1*) | |
let foo x y = x + y ;; | |
(* val foo : int -> int -> int = <fun> *) | |
foo 2 4;; | |
(* - : int = 6 *) | |
let foo x y = x * y in foo 2 4;; | |
(* - : int = 8 *) | |
foo 2 4;; | |
(* - : int = 6 *) | |
(* Shadowing and binding *) | |
let a = 10;; | |
let f x = x + a ;; | |
f 1 ;; (* 11*) | |
let a = 100 ;; (* New binding which shadows the old one - f "closes" a due to lexical scoping*) | |
f 1 ;; (* 11 *) | |
let sq = fun x -> x * x | |
(* Simplified form *) | |
let sq x = x * x | |
let sum = fun i -> (fun j -> i + j) | |
let sum i j = i + j | |
let sum i = | |
let sum2 j = | |
i + j in | |
sum2 | |
(* sum of first n *) | |
let rec sum_of_n n acc = | |
if n = 0 then acc | |
else (sum_of_n (n - 1) (acc + n)) ;; | |
(* Not tail-recursive *) | |
let rec sum_of_n n = | |
if n = 0 then n | |
else n + sum_of_n (n - 1) ;; | |
(* keyword args *) | |
let foobar ~x:i ~y:j = i - j;; | |
let foobar ~x ~y = x + y ;; | |
(********** PATTERN MATCHING pg 31/41***********) | |
match coll with | |
[] -> true | |
| x :: xs -> false ;; | |
let is_empty coll = | |
match coll with | |
[] -> true | |
| x :: xs -> false ;; | |
(* Simplified form *) | |
let is_empty = | |
function | |
| [] -> true | |
| x :: xs -> false ;; | |
(* "as" keywords *) | |
let is_char_a = | |
function | |
| ('a'|'A') as c -> true (* TUPLE PATTERN *) | |
| _ -> false ;; | |
(* "when" keyword *) | |
let rec last = | |
function | |
| [] -> raise Empty_list | |
| x :: xs when xs = [] -> x | |
| _ :: xs -> last xs ;; | |
(* SPECIFYING PARAMETER TYPES *) | |
let sum (x : int) (y : int) = x + y ;; | |
(* SPECIFYING FUNCTION RETURN TYPE *) | |
let sum x y : int = x + y ;; | |
let tuple = (1,2,4,4) ;; | |
let tuple = ('a',"asdf",4,4.0) ;; | |
(* CONS'ing *) | |
let lst = "a" :: [] ;; (* => ["a"] *) | |
let lst = ["a", "s", "d"] ;; | |
(* EQUIVALENT TO *) | |
let lst = "a" :: "b" :: "c" :: [];; | |
(* UNIONS *) | |
type number = | |
| Zero | |
| Integer of int | |
| Float of float ;; | |
(* Balanced Binary Tree *) | |
type 'a tree = | |
Leaf | |
| Node of 'a * 'a tree * 'a tree ;; | |
let rec cardinality = | |
function | |
| Leaf -> 0 | |
| Node (_, left, right) -> 1 + cardinality left + cardinality right;; | |
let rec cardinality' (t : 'a tree) = | |
match t with | |
| Leaf -> 0 | |
| Node (_, left, right) -> 1 + cardinality' left + cardinality' right;; | |
let rec cardinality' t = | |
match t with | |
| Leaf -> 0 | |
| Node (_, left, right) -> 1 + cardinality' left + cardinality' right;; | |
let rec card t = | |
function | |
| Leaf -> 0 | |
| Node (_, left, right) -> 1 + card left + card right;; | |
(* The above expression throws the following error. WHY ??????! | |
Error: This expression has type 'a tree -> int | |
but an expression was expected of type int | |
*) | |
(* Open Union types *) | |
let s_to_n s = | |
match s with | |
`Integer x -> 1 | |
| _ -> raise (Invalid_argument "unknown number");; | |
(* References *) | |
let flag = ref false;; | |
let flag := true;; |
Understood! Wah! We thanks it marteen sar!
It was trying to add 1 + ('a tree). Hence the error. Don't know how I missed it :)
no, it was trying to add 1 + (function Leaf -> 0 | Node -> 1 + card left + card right)
. the function wasn't being applied to anything, it was the return value of card.
oh yes. It was trying to add 1 + the function (which is of type 'a tree -> int)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when you say
note that
card t
is returning the function, and is therefore of type'a tree -> int
. this works:or more clearly