Rekumaru
Rekumaru

Reputation: 441

Did the OCaml compiler change drastically since RWO?

Example from Real World OCaml Page 60

List.map ~f:((+) 3) [4;5;6;];;
Error: The function applied to this argument has type 'a list -> 'b list
This argument cannot be applied with label ~f

Same example from the HTML hosted version of RWO

List.map ~f:((+) 3) [4;5;6];;
- : int list = [7; 8; 9]
Error: The function applied to this argument has type 'a list -> 'b list
This argument cannot be applied with label ~f

Obviously something significant changed right? Why don't these examples work? is there a better book to learn from?

This language is fantastic I would like to learn all I can but the resources are scarce.

Upvotes: 2

Views: 162

Answers (3)

ivg
ivg

Reputation: 35210

In order to use Core, and other Janestreet libraries you should open the umbrella module of the library. For example, for Core library, a substitute for a standard library, you need to start your module with

open Core.Std

This is how library is designed. You should admit it. This operation will prepare your environment for a proper use of the library. Do not try to use any tricks, like binding Core.Std to other module or anything else.

Upvotes: 0

Drup
Drup

Reputation: 3739

No, nothing was changed. Backward compatibility is taken very seriously in the OCaml community. :)

RWO uses a library called "core". There are some differences, in particular the f label on List.map. Apparently, you don't have it loaded.

There is a guide on how to setup everything in the prelude of the book. In the toplevel, you can do #require "core".

See also core's documentation and the stdlib's documentation.

Upvotes: 3

antron
antron

Reputation: 3847

Try using module ListLabels instead of List.

This is a question about the standard library and not the compiler, and this hasn't changed since RWO was published. RWO is using Jane Street Core, which has functions similar to ListLabels in the standard library. In particular, ListLabels and Jane Street Core List both have a label f on the function argument to map, whereas standard List does not.

Standard ListLabels

Standard List

Jane Street Core List

Search in your browser for val map on each one of those pages to see the function signatures.

You can see that RWO is using Jane Street Core from the statement # open Core.Std;; at the top of RWO code. If you want to use the regular standard module List, do

List.map ((+) 3) [4;5;6];;

Not sure about what is going on with their online top-level.

Upvotes: 7

Related Questions