English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    
Browse thread
Re: [Caml-list] Breaking out of iterative loops
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Krishnaswami, Neel <neelk@c...>
Subject: Re: [Caml-list] Breaking out of iterative loops
Markus Mottl [mailto:markus@oefai.at] wrote:
> On Wed, 01 May 2002, John Prevost wrote:
> > The drawback to allowing:
> > 
> > let rec ones = 1 :: ones
> > 
> > and such expressions is that when looking at the definition of, for
> > example, 'a list and length, one would expect it to be 
> > guaranteed that length terminates. 
> 
> I second this. Does anybody here really benefit from such cyclic
> structures?

Somewhat to my surprise, I can answer yes:

type expr =
  | Var of string
  | App of expr * expr
  | Fun of string * expr
  | Rec of string * string * expr
and denot = Closure of string * expr * env
and env = (string * denot) list

let rec eval e r =
  match e with
  | Var v -> List.assoc v r
  | App(f, arg) ->
      let Closure(n, body, r') = eval f r in
      let v = eval arg r in
      eval body ((n, v) :: r')
  | Fun(n, body) -> Closure(n, body, r)
  | Rec(name, n, body) -> 
      let rec c = Closure(n, body, r')
      and r' = (name, c) :: r in
      c

Using cyclic structures means I don't have to dink around with 
mutable references and all the headaches they bring, and so it's
very handy when prototyping. 

--
Neel Krishnaswami
neelk@cswcasa.com
-------------------
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