caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Frederic van der Plancke <fvdp@decis.be>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Function composition in CAML
Date: Tue, 10 Jun 2003 10:49:15 +0200	[thread overview]
Message-ID: <3EE59B8B.81D727FB@decis.be> (raw)
In-Reply-To: <000a01c32db1$041915c0$0100a8c0@r2d2>

> I have made some experimentations, but I can't find where is a bug in a 
> simple piece of code

> I represent a polynome by a list of its coefficents
[...]
> let horner p x =
>   let v= Array.of_list p in
>   let n = Array.length v in
>   let r = ref n in 
>   let f = ref (function u ->u  ) in
>     while !r <> 0 do
>       f := (function u -> !f( v.(!r)+ x*u));
>       r := !r -1 ;
>     done;
>     !f(0)
> ;;
> 
> In theory, the !f(0) call shall give me P(x)...
> But it seems that the computer crash, and can't handle this line of code...
> 
> Someone has an idea?

Subtle !
There is an infinite recursion: operator "!" before "f" is evaluated when
the code where it lies is evaluated, that is when f is called, 
hence "!f" represents the last created function f and not the one you want. 
You should evaluate "!f" outside of (function u -> ....), or (better) 
rewrite your loop using a recursive function taking "the current r" and
(maybe) "the current f" as argument(s).

(There's also an independent bug which will yield an out-of-bound array 
access error...)

Frédéric vdP

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


      parent reply	other threads:[~2003-06-10  8:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-08 11:27 TBraibant
2003-06-08 13:11 ` Oleg Trott
2003-06-10  8:49 ` Frederic van der Plancke [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=3EE59B8B.81D727FB@decis.be \
    --to=fvdp@decis.be \
    --cc=caml-list@inria.fr \
    --cc=fplancke@hotmail.com \
    /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).