caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Seeking pratical tutorial or examples.
       [not found] <20001019101803A.garrigue@kurims.kyoto-u.ac.jp>
@ 2000-10-19 19:02 ` Francisco Reyes
  2000-10-19 23:55   ` multiple destructuring? Bruce Hoult
  0 siblings, 1 reply; 3+ messages in thread
From: Francisco Reyes @ 2000-10-19 19:02 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

On Thu, 19 Oct 2000 10:18:03 +0900, Jacques Garrigue wrote:

>Caml-light has a wonderful example directory in the distribution.
>I join the README as attachment. Even as is, this can help you.


Thanks for the README.
 Pierre mentioned he is going to port the examples to Ocaml.

>Actually, the only examples in the distribution is a few samples
>in the otherlibs/labltk subdirectory, that I'm afraid very few
>people really know about.

They also may not be a good entry point for total newcomers.
The examples from the CamlLight distribution seem like the right
type. Simple yet complete enough to see a combination of
different topics (loops, input from user, etc..).


>There are good reasons why Objective Caml documentation cannot match
>that of bigger projects,

Which are? 
Need for more volunteers?
It is a cycle. The easier we make it for new people to join, the
more people we have to help with the documentation.

>but since plenty of examples are available,
>just making them easier to find would be a big help to beginner and
>intermediate programmers.

Yes. Much of the documentation would make much more sense if one
had a few examples to read. This is particularly the case for
the syntax of functions on the Standard Library (Chaper 19 of
the Ocaml Manual)
>
>Jacques Garrigue
>---------------------------------------------------------------------------
>Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
>		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>
>



francisco
Moderator of the Corporate BSD list
http://www.egroups.com/group/BSD_Corporate




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

* multiple destructuring?
  2000-10-19 19:02 ` Seeking pratical tutorial or examples Francisco Reyes
@ 2000-10-19 23:55   ` Bruce Hoult
  2000-10-20 12:18     ` Pierre Weis
  0 siblings, 1 reply; 3+ messages in thread
From: Bruce Hoult @ 2000-10-19 23:55 UTC (permalink / raw)
  To: caml-list

I'm just starting to play with OCaml and I wrote the following function:

let rec is_sorted lst =
    match lst with
       [] -> true
    | [elt] -> true
    | a :: b :: tail -> a<b & is_sorted(b :: tail)


I'm wondering whether the b :: tail actually allocates memory or is 
the compiler smarter than that?  What's the best way to avoid this? 
If I rewrite to match instead with "a :: tail" (and then later pull 
that apart into b :: more_tail) am I guaranteed that tail is 
non-empty because of the prior [elt] case?

-- Bruce



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

* Re: multiple destructuring?
  2000-10-19 23:55   ` multiple destructuring? Bruce Hoult
@ 2000-10-20 12:18     ` Pierre Weis
  0 siblings, 0 replies; 3+ messages in thread
From: Pierre Weis @ 2000-10-20 12:18 UTC (permalink / raw)
  To: Bruce Hoult; +Cc: caml-list

> I'm just starting to play with OCaml and I wrote the following function:
> 
> let rec is_sorted lst =
>     match lst with
>        [] -> true
>     | [elt] -> true
>     | a :: b :: tail -> a<b & is_sorted(b :: tail)
> 
> 
> I'm wondering whether the b :: tail actually allocates memory or is 
> the compiler smarter than that?

Yes it does, since you explicitely asked for it.

>  What's the best way to avoid this? 

Name the input sub-pattern (b :: tail) and use it in place of an
explicit call to "::" :

let rec is_sorted lst =
  match lst with
  | [] -> true
  | [elt] -> true
  | a :: (b :: tail as first_tail) -> a < b && is_sorted first_tail

> If I rewrite to match instead with "a :: tail" (and then later pull 
> that apart into b :: more_tail) am I guaranteed that tail is 
> non-empty because of the prior [elt] case?

Yes and no. If you write:

  match lst with
  | [] -> true
  | [elt] -> true
  | a :: tail -> ...

then you are guaranted that tail cannot be empty (this is witnessed by
the compiler that cheks it and does not emit any ``partial match''
warning).

However, if you further match the tail pattern in the right hand part
of the clause, the compiler ``forgets'' this fact and performs a new
exhaustivity check from scratch:

let rec is_sorted lst =
  match lst with
  | [] -> true
  | [elt] -> true
  | a :: tail ->
     begin match tail with
     | b :: t -> a < b && is_sorted tail
     end;;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
val is_sorted : 'a list -> bool = <fun>

Anyway, I would suggest a simpler solution to implement this function:

let rec is_sorted = function
  | a :: (b :: _ as t) -> a < b && is_sorted t
  | _ -> true;;

Best regards,

PS: If you allow duplicated elements in lists you should use <=
instead of < ...

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/




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

end of thread, other threads:[~2000-10-20 12:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20001019101803A.garrigue@kurims.kyoto-u.ac.jp>
2000-10-19 19:02 ` Seeking pratical tutorial or examples Francisco Reyes
2000-10-19 23:55   ` multiple destructuring? Bruce Hoult
2000-10-20 12:18     ` Pierre Weis

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