* [Caml-list] stack
@ 2001-02-13 22:24 maulin shah
2001-02-14 0:29 ` Gauthier Nadji
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: maulin shah @ 2001-02-13 22:24 UTC (permalink / raw)
To: caml-list
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???
thanks
maulin
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] stack
2001-02-13 22:24 [Caml-list] stack maulin shah
@ 2001-02-14 0:29 ` Gauthier Nadji
2001-02-14 7:30 ` Ohad Rodeh
2001-02-14 17:34 ` wester
2 siblings, 0 replies; 4+ messages in thread
From: Gauthier Nadji @ 2001-02-14 0:29 UTC (permalink / raw)
To: maulin shah; +Cc: caml-list
you define stack as a type and use it in "compile" as a variable.
in the function "compile", the variable "stack" was not declared.
you probably meant this:
#let rec compile e =
match e with
Num x -> [PUSH x]
|Opn (e1, o, e2) -> (compile e1) @ (compile e2)
@ [match o with Add -> IADD | Sub -> ISUB];;
-: val compile : exp -> ins list = <fun>
# compile (Opn (Num 5., Add, Num 2.));;
- : ins list = [PUSH 5.000000; PUSH 2.000000; IADD]
the problem is that this function is O(n^3).
this could be done in O(n), which is left as an exercise to
the student :)
hope this helps despite my obvious lack of pedagogy
On Tue, 13 Feb 2001, maulin shah wrote:
> 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???
>
> thanks
> maulin
>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35
> a year! http://personal.mail.yahoo.com/
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] stack
2001-02-13 22:24 [Caml-list] stack maulin shah
2001-02-14 0:29 ` Gauthier Nadji
@ 2001-02-14 7:30 ` Ohad Rodeh
2001-02-14 17:34 ` wester
2 siblings, 0 replies; 4+ messages in thread
From: Ohad Rodeh @ 2001-02-14 7:30 UTC (permalink / raw)
To: maulin shah; +Cc: caml-list
Maulin,
You have not defined the "stack" variable in your
code, therefore the error message.
May I add that I vote Piere should continue with
his thankless job of moderating this list,
Just my two cents,
Ohad.
----------------------------------------------------------
orodeh@cs.huji.ac.il
www.cs.huji.ac.il/~orodeh
On Tue, 13 Feb 2001, maulin shah wrote:
> 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???
>
> thanks
> maulin
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] stack
2001-02-13 22:24 [Caml-list] stack maulin shah
2001-02-14 0:29 ` Gauthier Nadji
2001-02-14 7:30 ` Ohad Rodeh
@ 2001-02-14 17:34 ` wester
2 siblings, 0 replies; 4+ messages in thread
From: wester @ 2001-02-14 17:34 UTC (permalink / raw)
To: maulin shah; +Cc: caml-list
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2001-02-14 22:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-13 22:24 [Caml-list] stack maulin shah
2001-02-14 0:29 ` Gauthier Nadji
2001-02-14 7:30 ` Ohad Rodeh
2001-02-14 17:34 ` wester
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox