User Tools

Site Tools


cs_lang:ocaml

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
cs_lang:ocaml [2011/07/12 09:28] cedriccs_lang:ocaml [2012/04/29 11:43] cedric
Line 1: Line 1:
 +====== Presentation ======
 +===== Paradigm ====
 +  * multi-paradigm: imperative, functional, object-oriented.
 +
 +===== Typing discipline =====
 +  * static typed;
 +  * strong types;
 +  * inferred.
 +
 +
 +====== Learning Objective Caml ======
 +  * [[http://caml.inria.fr/ocaml/ | Official Website]];
 +  * [[http://caml.inria.fr/pub/docs/oreilly-book/ | Developing Applications With Objective Caml]] (in [[http://www.pps.jussieu.fr/Livres/ora/DA-OCAML/ | french]]).
 +
 +
 ====== Simple examples ====== ====== Simple examples ======
 +===== Increment a number =====
 +<code ocaml>
 +# let inc n = n+1;;
 +val inc : int -> int = <fun>
 +
 +# inc 2;;
 +- : int = 3
 +
 +
 +# Alternative:
 +# let inc = (+) 1;;
 +val inc : int -> int = <fun>
 +</code>
 +
 +==== Equivalent in Python ====
 +<code python>
 +>>> def inc(n):
 +...     return n+1
 +
 +>>> inc(2)
 +3
 +</code>
 +
 +===== Great common divisor =====
 <code ocaml> <code ocaml>
-let rec pgcd a b = match (a mod b) with+let rec gcd a b = match (a mod b) with
   | 0 -> b   | 0 -> b
-  | c -> pgcd b c;;+  | c -> gcd b c;; 
 +val gcd : int -> int -> int = <fun> 
 + 
 +# gcd 77 42;; 
 +- : int = 7
 </code> </code>
  
 +==== Equivalent in Python ====
 +<code python>
 +def gcd(a, b):
 +    while b != 0:
 +        (a, b) = (b, a%b)
 +    return a
 +</code>
 +===== Parity =====
 <code ocaml> <code ocaml>
-let rec func = function+let rec parity = function
   | [] -> []   | [] -> []
-  | x::-> (x mod 2 == 0)::(func t);; +  | x::-> (x mod 2 == 0)::(parity l);; 
-val func : int list -> bool list = \<fun\>+val func : int list -> bool list = <fun> 
 + 
 +parity [1;2;3;4;5;6;7;8;9];; 
 +- : bool list = [false; true; false; true; false; true; false; true; false]
 </code> </code>
  
 +==== Equivalent in Python ====
 <code python> <code python>
 >>> l = [1,2,3,4,5,6,7,8,9] >>> l = [1,2,3,4,5,6,7,8,9]
 >>> [(a % 2 == 0) for a in l] >>> [(a % 2 == 0) for a in l]
 [False, True, False, True, False, True, False, True, False] [False, True, False, True, False, True, False, True, False]
-<code>+</code>
  
 +===== Ackermann =====
 <code ocaml> <code ocaml>
 let rec ackermann x y = match (x,y) with let rec ackermann x y = match (x,y) with
Line 26: Line 82:
 </code> </code>
  
 +==== Equivalent in Python ====
 +<code python>
 +def ackermann(m, n):
 +    if m == 0:
 +        return n + 1
 +    elif n == 0:
 +        return ackermann(m - 1, 1)
 +    else:
 +        return ackermann(m - 1, ackermann(m, n - 1))
 +</code>
 +
 +===== Factorielle =====
 <code ocaml> <code ocaml>
 let rec factorielle = function let rec factorielle = function
cs_lang/ocaml.txt · Last modified: 2012/04/29 11:44 by cedric