Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] stack
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: wester@i...
Subject: Re: [Caml-list] stack

> hi all,
> I am a student and learning ocaml. need help for one
> problem of Unbound value stack
> here is my code.
> 
> type ins =PUSH of float| IADD|ISUB;;
> type stack = ins list;;
> type opn = Add|Sub;;
> type exp = Num of float | Opn of exp* opn *exp;;
> 
> let rec compile exp=
> match exp with 
> Num x-> (PUSH x)::stack
> |Opn e1,Add,e2 -> compile e2,
>                   compile e1,
>                   (IADD)::stack;;
> 
> gives error : unbound value stack????
>  could any one clrify this???
> 

I'm an OCaml beginner too but I hope this is what you wanted.
(Any other solutions?) 

type ins = PUSH of float | IADD | ISUB;;
type stack = ins list;;
type opn = Add|Sub;;
type exp = Num of float | Opn of exp * opn * exp;;

let rec compile exp =
  match exp with 
	  Num x            -> (PUSH x)::[]
	| Opn (e1,Add,e2)  -> List.append (List.append (compile e1) (compile e2)) (IADD::[]);
	| Opn (e1,Sub,e2)  -> List.append (List.append (compile e1) (compile e2)) (ISUB::[]);;

let m = Opn (Opn (Num 2.0, Sub, Num 3.0), Add, Num 2.0);;
let stackn = compile m;;

Rolf Wester


-------------------------------------
Rolf Wester
wester@ilt.fhg.de
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr