180 likes | 412 Views
OCaml Modules. Modules. Library functions are defined in modules You can use functions from open modules # length [1;2;3];; - : int = 3 You can also use functions from unopened modules by prefixing the module name: # List.length [1;2;3];; - : int = 3. Opening modules.
E N D
Modules • Library functions are defined in modules • You can use functions from open modules # length [1;2;3];;- : int = 3 • You can also use functions from unopened modules by prefixing the module name: # List.length [1;2;3];;- : int = 3
Opening modules • You can open a module as follows: # open List;; • Opening one module may hide functions from other modules: # open String;; # length "abcde";; - : int = 5 # length [1; 2; 3];; Characters 7-16: This expression has type 'a list but is here used with type string # List.length [1; 2; 3];; - : int = 3
The Pervasives module • Many common functions are defined in the Pervasives module • The Pervasives module is automatically opened for you • You should probably open any other modules that you use a lot • The open statement can be put directly in your .ml file
List functions in Pervasives • Appending two lists: # [1;2;3] @ [4; 5; 6; 7];; • : int list = [1; 2; 3; 4; 5; 6; 7] • That's all
List functions in List: I # length ['a'; 'b'; 'c'];; - : int = 3 # hd ['a'; 'b'; 'c'];; - : char = 'a' # tl ['a'; 'b'; 'c'];; - : char list = ['b'; 'c'] # nth ['a'; 'b'; 'c'] 2;; - : char = 'c'
List functions in List: II # mem 'c' ['a'; 'b'; 'c'];; : bool = true # rev ['a'; 'b'; 'c'];; - : char list = ['c'; 'b'; 'a'] # rev_append ['a'; 'b'; 'c'] ['x'; 'y'; 'z'];; - : char list = ['c'; 'b'; 'a'; 'x'; 'y'; 'z'] # flatten [['a'; 'b'; 'c']; ['x'; 'y']; ['z']];; - : char list = ['a'; 'b'; 'c'; 'x'; 'y'; 'z']
Higher-order functions • A higher-order function is one that either • takes a function as an argument, or • returns a function as a value • (or both) • OCaml fully supports higher-order functions • For the next set of List functions, assume: # let even n = n mod 2 = 0;; val even : int -> bool = <fun>
List functions in List: III # for_all even [1;2;3;4;5];; - : bool = false # exists even [1;2;3;4;5];; - : bool = true # filter even [1;2;3;4;5];; - : int list = [2; 4] # partition even [1;2;3;4;5];; - : int list * int list = [2; 4], [1; 3; 5]
Constructing a list # let rec iota (m, n) = if m = n then [n] else m :: iota(m + 1, n);; val iota : int * int -> int list = <fun> # iota (5, 10);; - : int list = [5; 6; 7; 8; 9; 10] • What would iota (10, 5) do? • Does our iota function cover all the cases?
Char functions in Pervasives # int_of_char 'a';; - : int = 97 # char_of_int 97;; - : char = 'a'
Char functions in Char # escaped 'a';; - : string = "a" # escaped '\n';; - : string = "\\n" # uppercase 'a';; - : char = 'A' # lowercase 'A';; - : char = 'a'
String operations in Pervasives # "abc" ^ "xyz";; - : string = "abcxyz"
String operations in String: I # length "abcde";; - : int = 5 # create 8;; - : string = ",\t³\000èh\132\000" # make 8 '*';; - : string = "********" # get "abcde" 3;; - : char = 'd'
String operations in String: II # let abc = "abcde";; val abc : string = "abcde" # set abc 3 '*';; - : unit = () # abc;; - : string = "abc*e" # fill abc 1 3 '/';; - : unit = () # abc;; - : string = "a///e"
String operations in String: III # index "object-oriented" 'o';; - : int = 0 # rindex "object-oriented" 'o';; - : int = 7 # rindex "object-oriented" 'a';; Uncaught exception: Not_found. # contains "object-oriented" 'a';; - : bool = false
String operations in String: IV # - : uppercase "OCaml";; - : string = "OCAML" # lowercase "OCaml";; - : string = "ocaml" # capitalize "OCaml";; - : string = "OCaml" # uncapitalize "OCaml";; - : string = "oCaml"