Sorry, an error occurred while loading the content.

## Simply Question..Please Help/

Expand Messages
• Hi, I have to use lists for this and i can t figure out how to put an entry on my operand_stack (its defined as a ref list for access later. It seems simple
Message 1 of 4 , Mar 14 5:23 PM
• 0 Attachment
Hi,

I have to use lists for this and i can't figure out how to put an
entry on my operand_stack (its defined as a ref list for access
later. It seems simple but i've been trying to figure it out for 2
days now and nothing.

enter =
(*
in : integer x
out : x
mutates : operand_stack, adds x to top of stack
*)
(fun x -> x :: !operand_stack :: []; x);

this function tells me whats the first number on the stack, and seems
to work fine. im just showing what function i use to see it.

(fun () -> match !operand_stack with
[] -> 0
| x::list -> x);

thanks,
andrew
• ... I m not positive, but it seems like what you want to do is pass an integer to a function, cons that integer onto some global list ref, and then return the
Message 2 of 4 , Mar 14 6:21 PM
• 0 Attachment
On Mar 14, 2005, at 6:23 PM, A wrote:

> I have to use lists for this and i can't figure out how to put an
> entry on my operand_stack (its defined as a ref list for access
> later. It seems simple but i've been trying to figure it out for 2
> days now and nothing.

I'm not positive, but it seems like what you want to do is pass an
integer to a function, cons that integer onto some global list ref, and
then return the integer? That seems like an odd thing to do, but
sure...

let enter x =
(* replace the operand stack with x added onto the old stack *)
operand_stack := x::!operand_stack;
(* return x *)
x;;

> enter =
> (*
> in : integer x
> out : x
> mutates : operand_stack, adds x to top of stack
> *)
> (fun x -> x :: !operand_stack :: []; x);

The problem with this version you have here is that:
a: you're going to get a type error (well, you probably wont get a type
error, but the compiler will think you want to do something other than
what you think you are doing. The expression x :: !operand_stack ::
[]; says to retrieve the contents of the reference operand_stack, and
cons it on to the empty list, then to cons x onto the resulting list,
and then to throw away this resulting list and return the value of the
next expression. Note that in order for this to typecheck, if
operand_stack is a list ref, then x must also be a list containing the
same type -- this won't work if, say, operand_stack is an int list ref
and x is an int:

# let operand_stack = ref ([] : int list);;
val operand_stack : int list ref = {contents = []}
# let enter =
(fun x -> x :: !operand_stack :: []; x);;
Characters 25-50:
Warning: this expression should have type unit.
(fun x -> x :: !operand_stack :: []; x);;
^^^^^^^^^^^^^^^^^^^^^^^^^
val enter : int list -> int list = <fun>
# enter 5;;
Characters 6-7:
enter 5;;
^
This expression has type int but is here used with type int list

a: nowhere in there do you actually assign the updated list to the old
list's variable. You need to do that explicitly with the := operator.

William D. Neumann

"You've got Rita Marlowe in the palm of your hand."
"Palm of my hand? You haven't seen Rita Marlowe..."

-- Will Success Spoil Rock Hunter?
• Thank you so much! It was the := operator that I didn t know about. You re a Lifesaver Andrew ... 2 ... and ... type ... than ... and ... list, ... the ...
Message 3 of 4 , Mar 14 7:12 PM
• 0 Attachment
Thank you so much! It was the := operator that I didn't know about.

You're a Lifesaver

Andrew

--- In ocaml_beginners@yahoogroups.com, William D.Neumann
<wneumann@c...> wrote:
> On Mar 14, 2005, at 6:23 PM, A wrote:
>
> > I have to use lists for this and i can't figure out how to put an
> > entry on my operand_stack (its defined as a ref list for access
> > later. It seems simple but i've been trying to figure it out for
2
> > days now and nothing.
>
> I'm not positive, but it seems like what you want to do is pass an
> integer to a function, cons that integer onto some global list ref,
and
> then return the integer? That seems like an odd thing to do, but
> sure...
>
> let enter x =
> (* replace the operand stack with x added onto the old stack *)
> operand_stack := x::!operand_stack;
> (* return x *)
> x;;
>
> > enter =
> > (*
> > in : integer x
> > out : x
> > mutates : operand_stack, adds x to top of stack
> > *)
> > (fun x -> x :: !operand_stack :: []; x);
>
> The problem with this version you have here is that:
> a: you're going to get a type error (well, you probably wont get a
type
> error, but the compiler will think you want to do something other
than
> what you think you are doing. The expression x :: !
operand_stack ::
> []; says to retrieve the contents of the reference operand_stack,
and
> cons it on to the empty list, then to cons x onto the resulting
list,
> and then to throw away this resulting list and return the value of
the
> next expression. Note that in order for this to typecheck, if
> operand_stack is a list ref, then x must also be a list containing
the
> same type -- this won't work if, say, operand_stack is an int list
ref
> and x is an int:
>
> # let operand_stack = ref ([] : int list);;
> val operand_stack : int list ref = {contents = []}
> # let enter =
> (fun x -> x :: !operand_stack :: []; x);;
> Characters 25-50:
> Warning: this expression should have type unit.
> (fun x -> x :: !operand_stack :: []; x);;
> ^^^^^^^^^^^^^^^^^^^^^^^^^
> val enter : int list -> int list = <fun>
> # enter 5;;
> Characters 6-7:
> enter 5;;
> ^
> This expression has type int but is here used with type int list
>
> a: nowhere in there do you actually assign the updated list to the
old
> list's variable. You need to do that explicitly with the :=
operator.
>
> William D. Neumann
>
> "You've got Rita Marlowe in the palm of your hand."
> "Palm of my hand? You haven't seen Rita Marlowe..."
>
> -- Will Success Spoil Rock Hunter?
• ... As OCaml allows you to define your own binary infix operators, it is recommended that you put spaces between all operators. In this case: x ::
Message 4 of 4 , Mar 29 3:01 PM
• 0 Attachment
On Tuesday 15 March 2005 02:21, William D.Neumann wrote:
> let enter x =
> (* replace the operand stack with x added onto the old stack *)
> operand_stack := x::!operand_stack;
> (* return x *)
> x;;

As OCaml allows you to define your own binary infix operators, it is
recommended that you put spaces between all operators. In this case:

x :: !operand_stack

To ensure that the compiler distinguishes this from a "::!" operator (which it
won't in this case, so you happened to be ok).

Also, to the original poster - you may like to try a couple of things:

1. Use the Stack module to implement your imperative stack.

2. Ditch the imperative stack, all uses of "ref" and "mutable" and try handing
your state around as a function argument, i.e. use a purely functional style.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
Your message has been successfully submitted and would be delivered to recipients shortly.