1 / 18

OCaml Modules

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.

micol
Download Presentation

OCaml Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OCaml Modules

  2. 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

  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

  4. 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

  5. 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

  6. 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'

  7. 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']

  8. 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>

  9. 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]

  10. 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?

  11. Char functions in Pervasives # int_of_char 'a';; - : int = 97 # char_of_int 97;; - : char = 'a'

  12. Char functions in Char # escaped 'a';; - : string = "a" # escaped '\n';; - : string = "\\n" # uppercase 'a';; - : char = 'A' # lowercase 'A';; - : char = 'a'

  13. String operations in Pervasives # "abc" ^ "xyz";; - : string = "abcxyz"

  14. 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'

  15. 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"

  16. 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

  17. String operations in String: IV # - : uppercase "OCaml";; - : string = "OCAML" # lowercase "OCaml";; - : string = "ocaml" # capitalize "OCaml";; - : string = "OCaml" # uncapitalize "OCaml";; - : string = "oCaml"

  18. The End

More Related