caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] trouble w/ recursive let definitions.
@ 2004-08-15 17:05 Shishir Ramam
  2004-08-15 21:29 ` Brian Hurt
  0 siblings, 1 reply; 2+ messages in thread
From: Shishir Ramam @ 2004-08-15 17:05 UTC (permalink / raw)
  To: caml-list

Hi,
I might be missing something here, but recursive let definitions have me a 
little confused. The two cases below narrow down my problem. Why is case-2
invalid?

A reading of the Ocaml User manual did not suggest that case 2 should
be invalid.
I have pasted the relevent section of the manual. 

I am using Ocaml 3.07+2 precompiled win32 binaries. 

Thanks in advance for any help. 
-shishir


Case 1:
----------
let rec p1 s = 
    p2 s
and p2 s = 
    print_string s;;

This works fine. 

Case 2:
----------
let q0 i = 
    print_int i;
and rec q1 s = 
    q2 s
and q2 s = 
    print_string s;;

Characters 33-36:
  and rec q1 s = 
      ^^^
Syntax error

------------------------------
>From the User manual (3.07+2/win32) , Section 6.7.1 pg 109

Recursive definitions of names are introduced by let rec:
       let rec pattern1 = expr1 and . . . and patternn = exprn in expr
The only difference with the let construct described above is that the bindings
of names to values performed by the pattern-matching are considered already
performed when the expressions expr1 to exprn are evaluated. That is, the 
expressions expr1 to exprn can reference identifiers that are bound by one of 
the patterns pattern1, . . . , patternn, and expect them to have the same value
as in expr, the body of the let rec construct.
The recursive definition is guaranteed to behave as described above if the 
expressions expr1 to exprn are function definitions (fun . . . or
function . . .),
and the patterns pattern1 . . . patternn are just value names, as in:
       let rec name1 = fun . . . and . . . and namen = fun . . . in expr
This defines name1 . . . namen as mutually recursive functions local to expr.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] trouble w/ recursive let definitions.
  2004-08-15 17:05 [Caml-list] trouble w/ recursive let definitions Shishir Ramam
@ 2004-08-15 21:29 ` Brian Hurt
  0 siblings, 0 replies; 2+ messages in thread
From: Brian Hurt @ 2004-08-15 21:29 UTC (permalink / raw)
  To: Shishir Ramam; +Cc: caml-list

On Sun, 15 Aug 2004, Shishir Ramam wrote:

> Hi,
> I might be missing something here, but recursive let definitions have me a 
> little confused. The two cases below narrow down my problem. Why is case-2
> invalid?

rec binds with the let, and not the function name.  This gets me 
occassionally as well.  So if you have:

let rec f ... = ...
and g ... = ...

both f and g are recursive functions.  All of the functions you define 
after with however many 'and' clauses are all recursive.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2004-08-15 21:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-15 17:05 [Caml-list] trouble w/ recursive let definitions Shishir Ramam
2004-08-15 21:29 ` Brian Hurt

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