16

I'm currently working through "Real Word OCaml", and one of the basic examples with named / labeled parameters doesn't seem to work (using utop 4.01.0):

let languages = ["OCaml"; "Perl"; "C"];;
List.map ~f:String.length languages;;

Produces:

Error: The function applied to this argument has type 'a list -> 'b list
This argument cannot be applied with label ~f

Whereas:

List.map String.length languages;;

Produces the expected output [5; 4; 1].

caml.inria.fr mentions that:

In the core language, as in most languages, arguments are anonymous.

Does this mean that I have to include some kind of external library to make this code work ?

EDIT Here's my ~/.ocamlinit file (as per the installation instructions for the book):

(* Added by OPAM. *)
let () =                
  try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
  with Not_found -> ()
;;

#use "topfind"
#camlp4o
#thread
#require "core.top"
#require "core.syntax" 
perror
  • 7,071
  • 16
  • 58
  • 85
Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
  • 1
    I would expect the library needed to make this example work is Core (an “Industrial strength alternative to OCaml's standard library” as listed on https://ocaml.janestreet.com/ocaml-core/latest/doc/ ). Does the book not mention something about installing it first? – Pascal Cuoq Feb 01 '14 at 13:26
  • As far as I can tell, I've installed Core correctly; see my updated answer. – Frank Schmitt Feb 01 '14 at 13:35
  • 8
    Did you put 'open Core.Std ;;' first ? – rafix Feb 01 '14 at 14:18
  • @rafix That did the trick. If you add this as an answer, I'll gladly accept it. – Frank Schmitt Feb 01 '14 at 16:25

2 Answers2

13

As mentioned in @rafix's comment, this can be fixed by putting

open Core.Std ;;

first.

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
2

The standard List.map method isn't defined with the label ~f. The type of List.map is ('a -> 'b) -> 'a list -> 'b list, but if you wanted to use the "~f" label, it would have to be "f:('a->'b) -> 'a list -> 'b list". If you wanted to define your own, you would have to define it as such:

 let rec myMap ~f l = match l with
 | [] -> []
 | h::t -> (f h) :: (myMap ~f t);;

 val myMap : f:('a -> 'b) -> 'a list -> 'b list = <fun>

and then you could call it like you wanted:

 myMap ~f:String.length languages

Cheers!

sacooper93
  • 86
  • 2