Category Archives: Ocaml
Pattern Matching in Ocaml
Posted by Shawn Zhang
on December 7, 2012
No comments
Pattern Matching is distinguish feature that own by ocaml, it's really powerful and saving a lot of type in code.
Matching with Value
Define in function with explict specifying "Match .. With" keyword
# let rec fib i = match i with
0 -> 0
| 1 -> 1
| j -> fib (j 2) + fib (j 1);;
Shorthand Matching within Function . function is same with fun with one parameter
# let rec fib = function
0 -> 0
| 1 -> 1
| i -> fib (i 1) + fib (i 2);;
val fib : int -> int = <fun>
cluster Read more [...]
Arrays / String in Ocaml
Posted by Shawn Zhang
on December 5, 2012
No comments
Disclaimer : code and structure most from <Introduction to Objective Caml, Jason Hickey>, <http://pleac.sourceforge.net/pleac_ocaml/> personally use this article to be served as a learning note.
Definition of Array:
Fixed Length ; Same Type, form : [| a; b; c; ....|], Mutable
Create a Array:
(1) explicitly specify elements in a array :
let a = [|1; 3; 5; 7|];;
(2) using function Arrary.create
# let a = Array.create 10 1;;
val a : int array = [|1; 1; 1; 1; 1; 1; 1; 1; Read more [...]
Records in Ocaml
Posted by Shawn Zhang
on December 4, 2012
No comments
It seems similar concept to structure in C . Record is a several data type mix structure with labeled names.
Definition
Example from <Introduction to Objective Caml>, with "name ""height" as Identifier followed by a colon and then a data type .
# type db_entry =
{ name : string;
height : float;
phone : string;
salary : float
};;
type db_entry = { name: string; height: float;
phone: string; salary: float }
Instantaneous a Record
# let jason =
{ name = "Jason";
height Read more [...]
Personal Notes of Ocaml(1)
Posted by Shawn Zhang
on November 28, 2012
No comments
Print a string with a "print_string"
print_string "Hello, World!\n";;
Import Printf function
use "printf.ml"
Printf.printf
”Assign“ a Value to so-call variable, using "let" . Type "char" quoted by a single quote “ ‘ ”(actually Ocaml didn't do that, left-hand stuff just a shorthand for the right-hand stuff, bcuz left-hand value can't be changed )
# let a = 1;;
# let foo = "Hello, World!"
# let foo = 'a';;
Difference between using Read more [...]