caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Difference between "let rec" and just "let"?
@ 2008-08-13 12:49 circ ular
  2008-08-13 13:52 ` [Caml-list] " Jean Krivine
  2008-08-13 15:17 ` Christophe TROESTLER
  0 siblings, 2 replies; 4+ messages in thread
From: circ ular @ 2008-08-13 12:49 UTC (permalink / raw)
  To: caml-list

what is the difference between "let rec" and just "let"? what does rec
stand for?

are the following defintions exactly the same? at least they seem to
give the same results...

# let rec cube x = x*x*x;;
val cube : int -> int = <fun>
# cube 12;;
- : int = 1728

# let cubex x = x*x*x;;
val cubex : int -> int = <fun>
# cubex 12;;
- : int = 1728
#


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

* Re: [Caml-list] Difference between "let rec" and just "let"?
  2008-08-13 12:49 Difference between "let rec" and just "let"? circ ular
@ 2008-08-13 13:52 ` Jean Krivine
  2008-08-13 18:39   ` Andrej Bauer
  2008-08-13 15:17 ` Christophe TROESTLER
  1 sibling, 1 reply; 4+ messages in thread
From: Jean Krivine @ 2008-08-13 13:52 UTC (permalink / raw)
  To: circ ular; +Cc: caml-list

Each time you use let, you can also use let rec, but you have to use
the rec (for recursive) if you want to declare a recursive function as
in:
let rec f x = if x<0 then 0 else x * f(x-1)


On Wed, Aug 13, 2008 at 8:49 AM, circ ular <circularfunc@gmail.com> wrote:
> what is the difference between "let rec" and just "let"? what does rec
> stand for?
>
> are the following defintions exactly the same? at least they seem to
> give the same results...
>
> # let rec cube x = x*x*x;;
> val cube : int -> int = <fun>
> # cube 12;;
> - : int = 1728
>
> # let cubex x = x*x*x;;
> val cubex : int -> int = <fun>
> # cubex 12;;
> - : int = 1728
> #
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


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

* Re: [Caml-list] Difference between "let rec" and just "let"?
  2008-08-13 12:49 Difference between "let rec" and just "let"? circ ular
  2008-08-13 13:52 ` [Caml-list] " Jean Krivine
@ 2008-08-13 15:17 ` Christophe TROESTLER
  1 sibling, 0 replies; 4+ messages in thread
From: Christophe TROESTLER @ 2008-08-13 15:17 UTC (permalink / raw)
  To: circularfunc; +Cc: OCaml Mailing List

On Wed, 13 Aug 2008 14:49:30 +0200, circ ular wrote:
> 
> what is the difference between "let rec" and just "let"? what does rec
> stand for?
> 
> are the following defintions exactly the same? at least they seem to
> give the same results...
> 
> # let rec cube x = x*x*x;;
> val cube : int -> int = <fun>
> # cube 12;;
> - : int = 1728
> 
> # let cubex x = x*x*x;;
> val cubex : int -> int = <fun>
> # cubex 12;;
> - : int = 1728
> #

The difference is that for [let x = e], [x] is not known in [e] (or
else, it is a previously defined [x]).  This allows the following
style (broadly used despite the thread on that topic):

  let r = .... in
  let r = .... r (* r of the previous line *) .... in
  let r = .... r (* r of the previous line *) .... in
  let r = .... r (* r of the previous line *) .... in
  ...

When using [let rec x = e], [x] is known in [e] which allows to define
recursive functions but also cyclic data :

  let rec x = 1 :: x

Hope it helps,
C.


P.S.  There exist a beginner list where these questions belong.


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

* Re: [Caml-list] Difference between "let rec" and just "let"?
  2008-08-13 13:52 ` [Caml-list] " Jean Krivine
@ 2008-08-13 18:39   ` Andrej Bauer
  0 siblings, 0 replies; 4+ messages in thread
From: Andrej Bauer @ 2008-08-13 18:39 UTC (permalink / raw)
  To: caml-list; +Cc: Jean Krivine

Jean Krivine wrote:
> Each time you use let, you can also use let rec,

Not quite. For example:

let x = 5 in
let x = x + 10 in
  x

is not equivalent to

let rec x = 5 in
let rec x = x + 10 in
  x

(In fact the second one doesn't compile.)

Andrej


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

end of thread, other threads:[~2008-08-13 18:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-13 12:49 Difference between "let rec" and just "let"? circ ular
2008-08-13 13:52 ` [Caml-list] " Jean Krivine
2008-08-13 18:39   ` Andrej Bauer
2008-08-13 15:17 ` Christophe TROESTLER

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