Created
March 23, 2016 14:15
-
-
Save dakk/08a038834a66fc0a2e9b to your computer and use it in GitHub Desktop.
OCaml various snippets
This file contains 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
#use "printf.ml";; | |
(* Type def *) | |
type json = | |
| Object of (string * json) list | |
| Bool of bool | |
| Float of float | |
| Int of int | |
| Null | |
| String of string | |
| List of json list | |
;; | |
let config = | |
Object [ | |
("gianni", Int 12); | |
("destination", String "/dev"); | |
("targets", | |
List [ | |
String "target1"; | |
String "target2"; | |
String "target3"; | |
] | |
) | |
] | |
;; | |
let rec print_list l = | |
match l with | |
| a::l' -> printf "%d " a; print_list l' | |
| [] -> printf "\n" | |
;; | |
let rec json_print js t = | |
let rec print_tab i = | |
match i with | |
| i when (i<1) -> () | |
| 0 -> () | |
| i -> printf "\t"; print_tab (i-1) | |
in | |
match js with | |
| Object ([]) -> | |
printf "\n"; | |
print_tab (t-1); | |
printf "}" | |
| Object ((n,v)::l') -> | |
printf "\"%s\": {" n; | |
json_print v (t+1); | |
printf "},\n"; | |
json_print (Object (l')) t | |
| String s -> printf "\"%s\"" s | |
| Int i -> printf "%d" i | |
| Float f -> printf "%f" f | |
| Null -> printf "null" | |
| Bool b -> printf "%b" b | |
| List ([]) -> () | |
| List (a::l) -> | |
json_print a t; | |
printf ", "; | |
json_print (List (l)) t | |
;; | |
json_print config 0;; | |
(* | |
let rec json_field_by_path js path = | |
let rec field l name = | |
match js with | |
| Object ((n,v)::l') -> if n=name then v else field l' name | |
| Object ([]) -> Null | |
in | |
match path with | |
| a::l' -> json_field_by_path (field js a) l' | |
| [] -> js | |
;; | |
*) | |
(* Map, filter and fold *) | |
List.map (fun x -> x*2) [1;2;3];; | |
List.filter (fun x -> x < 2) [1;4;0];; | |
List.fold_left (+) 0 [1;2;3];; | |
(* Recursive functions *) | |
let rec fact n = | |
match n with | |
| 1 -> 1 | |
| _ -> n * fact (n-1) | |
;; | |
(* Console output *) | |
printf "%d\n" (fact 60);; | |
(* Binary Tree *) | |
type 'a btree = | |
| BLeaf of 'a | |
| BNode of 'a btree * 'a btree | |
;; | |
let rec btree_lf root = | |
match root with | |
| BLeaf a -> [a] | |
| BNode (a, b) -> (btree_lf a)@(btree_lf b) | |
;; | |
let rec btree_rf root = | |
match root with | |
| BLeaf a -> [a] | |
| BNode (a, b) -> (btree_rf b)@(btree_rf a) | |
;; | |
let t = | |
BNode ( | |
BNode ( | |
BLeaf 1, | |
BLeaf 2 | |
), | |
BNode ( | |
BLeaf 3, | |
BNode ( | |
BLeaf 4, | |
BLeaf 5 | |
) | |
) | |
);; | |
print_list (btree_lf t);; | |
print_list (btree_rf t);; | |
(* Tree *) | |
type 'a tree = | |
| Node of 'a tree list | |
| Leaf of 'a | |
;; | |
let t = | |
Node [ | |
Node [ | |
Leaf 1; | |
Leaf 2; | |
Leaf 3; | |
Leaf 4 | |
]; | |
Node [ | |
Leaf 5; | |
Node [ | |
Leaf 6; | |
Leaf 7; | |
Leaf 8 | |
]; | |
Leaf 9 | |
] | |
] | |
;; | |
let rec tree_lf root = | |
match root with | |
| Node (a::l) -> (tree_lf a)@(tree_lf (Node (l))) | |
| Node [] -> [] | |
| Leaf a -> [a] | |
;; | |
print_list (tree_lf t);; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment