Understanding the imperative list example in Ocaml

I am new to okamla. appreciate if anyone can help me understand the material presented on page 94 of the book “caml lens application development”.

There is a problem understanding the meaning of the following paragraph:

It’s just that the evaluation (itl l) place before the evaluation (ihd l) , so that at the last iteration of imap list referenced by l became an empty list before we examined his head. The example list is now definitely empty, although we did not get any result.

The imap (function x ! x) example returns

 Uncaught exception: Failure("hd") 

instead

 - : string ilist = {c=["one"; "two"; "three"]} 

I would have thought that

 else icons (f (ihd l)) (imap f (itl l))` 

will become icons("one") ( ( icons("two") ( ( icon("three")([]) ) ) ) and return

 - : string ilist = {c=["one"; "two"; "three"]} 
+4
source share
1 answer

I am an example in a book, the list was implemented in an imperative style. The itl function mutates the list, i.e. Modifies the list by deleting the first item. After calling itl first element essentially went away forever.

The tricky part is the order in which the icons arguments are executed in the statement:

 else icons (f (ihd l)) (imap f (itl l)) 

The OCaml specification does not specify the order. The last time I checked the INRIA compiler, I ordered the last argument first, so (imap f (itl l)) is executed before (f (ihd l)) . This means that by the time ihd actually called, itl was called enough times to remove all elements of l .

As an example, consider the penultimate recursive call - l is just ["three"] . You think this will result in:

 icons (f "three") (imap f []) 

However, let's see what happens if you first call itl :

 (* l = ["three"] *) let tail = (itl l) in (* tail = [], l = [] *) let head = (ihd l) in (* l = [], ihd raises an exception *) icons head (imap f tail) 

As an example, try running this code:

 let fxyz = x + y + z f (print_int 1; 1) (print_int 2; 2) (print_int 3; 3) 

On my machine (OCaml 3.12) I get this output:

 321- : int = 6 
+4
source

Source: https://habr.com/ru/post/1332999/


All Articles