Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Are map and iter guaranteed to be called in forwards order?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Are map and iter guaranteed to be called in forwards order?
On Wed, 2004-08-18 at 15:11, skaller wrote:
> On Wed, 2004-08-18 at 10:57, Jon Harrop wrote:
> > On Tuesday 17 August 2004 15:26, John Prevost wrote:
> 
> > let index l =
> >   List.rev (snd (List.fold_left (fun (i, l) e -> i+1, (i, e)::l) (0, []) l));;
> 
> > Ha! Try writing that in Felix (in another mailing-list, of course ;-).
> 
> The actual code above doesn't compile due to a bug,

Oops. Its a bug in my translation of the routine, not in Felix!
The code below actually works. Clear advantages of Ocaml here are:

(a) Ocaml's type inference simplifies presentation
  
(b) :: as both constructor and pattern in Ocaml are nice,
   as is [x;y;z] list constructor
   (Felix uses :: like C++ for qualification :(

(c) Felix anonymous functions only accept one argument 
   (needs fixing)

(d) Felix function parameters aren't general patterns: at best
  a single top level tuple match is allowed (ugly constraint)

However the worst problem isn't shown by the correct code:
whilst Ocaml's error messages are sometimes confusing as 
to reason and location, and Felix is generally better at locating
the source of a problem -- Felix sometimes gives very confusing
explanations -- it took me an hour to finally convince myself
I actually had a type error in my previous code, rather than
a compiler bug.

The fact that an ordinary programmer can write a compiler
for a powerful language in Ocaml really shows just how good
Ocaml is :)

---------------------------------
include "std";
open List;

fun snd(x,y)=>y;
fun fst(x,y)=>x;

fun index[t] (l:list[t]) = {
  typedef il_t = int * list [int * t];
  fun f(il:il_t) (e: t) =>
    match il with
    | ?i,?l => i+1, Cons ((i, e),l)
    endmatch
  ;
  return
    rev (snd (
      fold_left
      f of (il_t)
      (0, Empty[int * t]) 
      l
    ))
  ;
}

var x = Empty[int];
x = Cons(11,x);
x = Cons(22,x);
x = Cons(33,x);
x = Cons(44,x);
x = Cons(55,x);
x = Cons(66,x);

val z = index x;
iter 
  (proc (x:int,y:int)
    {
      print x; print " -> "; print y; endl;
    }
  )
  z
;

------------
[skaller@pelican] ~/links/flx>bin/flx -Ilib --force il
0 -> 66
1 -> 55
2 -> 44
3 -> 33
4 -> 22
5 -> 11


-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



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