caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Mentre <David.Mentre@inria.fr>
To: Rick Bischoff <bischoff@rickjr.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Ocaml in depth?
Date: 14 Aug 2001 09:40:14 +0200	[thread overview]
Message-ID: <qtlofpj84yp.fsf@pochi.inria.fr> (raw)
In-Reply-To: <7278206975.20010813100247@rickjr.org>

Rick Bischoff <bischoff@rickjr.org> writes:

>       (1) The only "real" examples I see in the tutorials and books,
>       etc, are "parsing" problems.  This seems to be the topic at the
>       functional programming contests at lot (with a high probability
>       at least);  are there any other good examples?

ML is good at handling symbolic computation (compilers, analyzers,
etc.), so you'll find a lot of "parsing" problems. Historically, ML was
designed to program theorem provers.

That's said, Caml is much better nowadays at usual computations, for
example numeric computations with its native compiler and the Bigarray
module.

You'll find a good set of real world apps at:
  http://caml.inria.fr/users_programs-eng.html 


>       (2) How can I learn better to escape my simpleton method of
>       writing C code in Caml?

1. Read the FAQs:
  http://caml.inria.fr/FAQ/index-eng.html
Mainly:
  http://caml.inria.fr/FAQ/FAQ_DEBUTANT-eng.html
  http://caml.inria.fr/FAQ/FAQ_EXPERT-eng.html

2. Look at good code (general advice). For Caml, look at ocaml source
   code, for example the List module of standard library:
List.ml:
  http://camlcvs.inria.fr/cgi-bin/cvsweb.cgi/ocaml/stdlib/list.ml?rev=1.27&content-type=text/x-cvsweb-markup
List.mli:
  http://camlcvs.inria.fr/cgi-bin/cvsweb.cgi/ocaml/stdlib/list.mli?rev=1.30&content-type=text/x-cvsweb-markup

>       (3) Why do functions return (int -> int) -> int -> int when
>       the real type is (int -> int) -> (int -> int).  I find that
>       confusing (again, I am a simpleton)

Currying (the Magic Word(tm) ;-). It allows to handle partial
applications, that is to say functions that have parts of their
arguments.

For example, consider:
# let f g x = let x' = x + 1 in (g x') + 1;;
val f : (int -> int) -> int -> int = <fun>

You can use it with all its arguments:
# let h x = x - 1 in f h 3;;
- : int = 4

But you can also create a specialized function by giving only the first
parameter:
# let specialized x = let h x = x * 2 in f h x;;
val specialized : int -> int = <fun>

So, what it is useful for? It is very useful in combination with lists,
for example with List.iter or List.map:
# List.map specialized [ 1; 2; 3; 4];;
- : int list = [5; 7; 9; 11]

You see? It allows to prepare a computation and apply it "in batch" to a
data structure. Nearly all data structures (List, Hashtbl, ...) have
iter, fold operators (and other one).

>       (4) What is the difference between saying:
> 
>           let bob =
>               function [] -> []
>                        h :: t -> h
>           ;;
> 
>           and
> 
>           let fun john lst =
>               match lst with
>                     [] -> []
>               |     h::t -> h
>           ;;

You mean:
# let bob default = function
  | [] -> default
  | h :: _ -> h;;
val bob : 'a -> 'a list -> 'a = <fun>

and

# let john default = fun lst ->
  match lst with
    | [] -> default
    | h :: _ -> h;;  
val john : 'a -> 'a list -> 'a = <fun>

No difference.

# bob 3 [];;
- : int = 3
# john 3 [];;
- : int = 3
# bob 3 [ 4 ];;
- : int = 4
# john 3 [ 4 ];;
- : int = 4

In fact, 'function | [] ->...' is exactly the same as 'fun x -> match x
with | [] ->...'. In one case, the x parameter is implicit, in another
case, it is explicit.

Best regards,
d.
-- 
 David.Mentre@inria.fr
 Opinions expressed here are only mine.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


      reply	other threads:[~2001-08-14  7:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-13 15:02 Rick Bischoff
2001-08-14  7:40 ` David Mentre [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=qtlofpj84yp.fsf@pochi.inria.fr \
    --to=david.mentre@inria.fr \
    --cc=bischoff@rickjr.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).