caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] let rec with let ref
@ 2016-03-17 15:32 Antoine Rimlet
  2016-03-17 15:37 ` vrotaru.md
  0 siblings, 1 reply; 4+ messages in thread
From: Antoine Rimlet @ 2016-03-17 15:32 UTC (permalink / raw)
  To: caml-list

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

Hi,

I'm trying to write a recursive function that would work like a for loop:

let rec forloop g v u =
  let x = ref v in let rec h = begin g !x; x := !x + 1; if !x <= u then h
end
  in h;;

I know this is horribly far from the functional spirit, but anyway ocaml
complains that "This kind of expression is not allowed as right-hand side
of `let rec'". It seems to me that this function definition has a meaning,
so why is it rejected? What should I do to get it accepted by ocaml?

Many thanks in advance,

Antoine

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

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

* Re: [Caml-list] let rec with let ref
  2016-03-17 15:32 [Caml-list] let rec with let ref Antoine Rimlet
@ 2016-03-17 15:37 ` vrotaru.md
  2016-03-18 11:36   ` Goswin von Brederlow
  0 siblings, 1 reply; 4+ messages in thread
From: vrotaru.md @ 2016-03-17 15:37 UTC (permalink / raw)
  To: Antoine Rimlet, caml-list

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

let rec h () = ... then h ()...

will define (and apply a function)

Iet rec value = .. value

is something totally different

În joi, 17 mar. 2016 la 17:33, Antoine Rimlet <antoine.rimlet@gmail.com> a
scris:

> Hi,
>
> I'm trying to write a recursive function that would work like a for loop:
>
> let rec forloop g v u =
>   let x = ref v in let rec h = begin g !x; x := !x + 1; if !x <= u then h
> end
>   in h;;
>
> I know this is horribly far from the functional spirit, but anyway ocaml
> complains that "This kind of expression is not allowed as right-hand side
> of `let rec'". It seems to me that this function definition has a meaning,
> so why is it rejected? What should I do to get it accepted by ocaml?
>
> Many thanks in advance,
>
> Antoine
>
>

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

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

* Re: [Caml-list] let rec with let ref
  2016-03-17 15:37 ` vrotaru.md
@ 2016-03-18 11:36   ` Goswin von Brederlow
  2016-03-18 11:55     ` Christoph Höger
  0 siblings, 1 reply; 4+ messages in thread
From: Goswin von Brederlow @ 2016-03-18 11:36 UTC (permalink / raw)
  To: caml-list

On Thu, Mar 17, 2016 at 03:37:32PM +0000, vrotaru.md@gmail.com wrote:
> let rec h () = ... then h ()...
> 
> will define (and apply a function)
> 
> Iet rec value = .. value
> 
> is something totally different
> 
> În joi, 17 mar. 2016 la 17:33, Antoine Rimlet <antoine.rimlet@gmail.com> a
> scris:
> 
> > Hi,
> >
> > I'm trying to write a recursive function that would work like a for loop:
> >
> > let rec forloop g v u =
> >   let x = ref v in let rec h = begin g !x; x := !x + 1; if !x <= u then h
> > end
> >   in h;;
> >
> > I know this is horribly far from the functional spirit, but anyway ocaml
> > complains that "This kind of expression is not allowed as right-hand side
> > of `let rec'". It seems to me that this function definition has a meaning,
> > so why is it rejected? What should I do to get it accepted by ocaml?
> >
> > Many thanks in advance,
> >
> > Antoine

Also why use a ref? You need to define a function, not a value here anyway.

let rec h x = ... then h (x+1)
in h v

MfG
	Goswin

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

* Re: [Caml-list] let rec with let ref
  2016-03-18 11:36   ` Goswin von Brederlow
@ 2016-03-18 11:55     ` Christoph Höger
  0 siblings, 0 replies; 4+ messages in thread
From: Christoph Höger @ 2016-03-18 11:55 UTC (permalink / raw)
  To: caml-list

You should use this variant:

let forloop g v u =
  let x = ref v in let rec h () = begin g !x; x := !x + 1; if !x <= u
then h ()
end
  in h;;

1. forloop does not need to be recursive (and is not a syntactical function)
2. h is not a syntactical function, but can easily be made one.
Recursive functions need to be syntactical functions (i.e.
lambda-abstractions) in order to correctly compile them.

regards,

Christoph

Am 18.03.2016 um 12:36 schrieb Goswin von Brederlow:
> let rec forloop g v u =
>> >   let x = ref v in let rec h = begin g !x; x := !x + 1; if !x <= u then h
>> > end
>> >   in h;;


-- 
Christoph Höger

Technische Universität Berlin
Fakultät IV - Elektrotechnik und Informatik
Übersetzerbau und Programmiersprachen

Sekr. TEL12-2, Ernst-Reuter-Platz 7, 10587 Berlin

Tel.: +49 (30) 314-24890
E-Mail: christoph.hoeger@tu-berlin.de

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

end of thread, other threads:[~2016-03-18 11:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-17 15:32 [Caml-list] let rec with let ref Antoine Rimlet
2016-03-17 15:37 ` vrotaru.md
2016-03-18 11:36   ` Goswin von Brederlow
2016-03-18 11:55     ` Christoph Höger

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