caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] short-circuit in Real World Ocaml ch 3 deserves a comment
@ 2013-11-19 15:05 Jean Saint-Remy
  2013-11-19 22:11 ` Nicolas Braud-Santoni
  2013-11-25  1:36 ` Francois Berenger
  0 siblings, 2 replies; 3+ messages in thread
From: Jean Saint-Remy @ 2013-11-19 15:05 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 935 bytes --]

Hi,

I found a missing "does not change" sentence fragment in the third section on Lists and Patterns


"Each:: essentially adds a new block to the proceding picture. Such a block contains two things: a reference to the data in that list element, and a reference to the remainder of the list. This is why :: can extend a list without modifying it; extension allocates a new list element but {here} change any of the existing ones, as you can see: "


The example code using pattern matching appears to be a "let rec" short-circuit which I believe deserves a comment in the book. It appears to be puzzling, we are binding a "zero" to the head of the list in order to drop it? I think it is a fine example of OCaml that should be encouraged, not just an isolated case.

let rec drop_zero ls = match ls with
  | [] -> [] 

  | 0 :: tl -> drop_zero tl
  | hd :: tl -> hd :: drop_zero tl 

;;

With kind regards,

Jean

[-- Attachment #2: Type: text/html, Size: 1408 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] short-circuit in Real World Ocaml ch 3 deserves a comment
  2013-11-19 15:05 [Caml-list] short-circuit in Real World Ocaml ch 3 deserves a comment Jean Saint-Remy
@ 2013-11-19 22:11 ` Nicolas Braud-Santoni
  2013-11-25  1:36 ` Francois Berenger
  1 sibling, 0 replies; 3+ messages in thread
From: Nicolas Braud-Santoni @ 2013-11-19 22:11 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 890 bytes --]

On 19/11/2013 16:05, Jean Saint-Remy wrote:
> The example code using pattern matching appears to be a "let rec"
> short-circuit which I believe deserves a comment in the book. It
> appears to be puzzling, we are binding a "zero" to the head of the
> list in order to drop it? I think it is a fine example of OCaml that
> should be encouraged, not just an isolated case.
>
> let rec drop_zero ls = match ls with
>   | [] -> []
>   | 0 :: tl -> drop_zero tl
>   | hd :: tl -> hd :: drop_zero tl
> ;;

Hi,

It isn't a « short circuit » : the list is pattern matched, and if it
matches `0 :: tl`, then the result is `drop_zero tl`.
The head of the list (0) is effectively dropped by not including it on
the right hand side.

And, as far as I know, it is rather idiomatic to use pattern matching in
the way (when matching over suitable datatypes).


Regards,
Nicolas


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] short-circuit in Real World Ocaml ch 3 deserves a comment
  2013-11-19 15:05 [Caml-list] short-circuit in Real World Ocaml ch 3 deserves a comment Jean Saint-Remy
  2013-11-19 22:11 ` Nicolas Braud-Santoni
@ 2013-11-25  1:36 ` Francois Berenger
  1 sibling, 0 replies; 3+ messages in thread
From: Francois Berenger @ 2013-11-25  1:36 UTC (permalink / raw)
  To: caml-list

On 11/20/2013 12:05 AM, Jean Saint-Remy wrote:
> [...]
> The example code using pattern matching appears to be a "let rec"
> short-circuit which I believe deserves a comment in the book. It appears
> to be puzzling, we are binding a "zero" to the head of the list in order
> to drop it? I think it is a fine example of OCaml that should be
> encouraged, not just an isolated case.
>
> let rec drop_zero ls = match ls with
>    | [] -> []
>    | 0 :: tl -> drop_zero tl
>    | hd :: tl -> hd :: drop_zero tl
> ;;

Are we really speaking of some "real world" OCaml?

This code is not tail recursive and can be replaced
by a one liner using List.filter from the standard library.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-11-25  1:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-19 15:05 [Caml-list] short-circuit in Real World Ocaml ch 3 deserves a comment Jean Saint-Remy
2013-11-19 22:11 ` Nicolas Braud-Santoni
2013-11-25  1:36 ` Francois Berenger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).