Discussion:
SML please for translation
(too old to reply)
stainboy
2007-09-26 20:00:56 UTC
Permalink
Hello. I'm trying to learn something but i have big problems. Could
you translate me this program:


type var = string;
datatype exp =
N of int
| V of var;

type value = int;
type state = var -> value;

fun expSem
(N i)(s) = i
| expSem (V v)(s:state) = s v;


what mean: (N i)(s) = i
also:
(V v)(s:state) = s v;


Thank you verry much for help.
Chris Rathman
2007-10-01 19:45:02 UTC
Permalink
Probably get a better and more concise explanation from others, but
here's a quick stab.

The type of exp is a union of either an integer or a string. N and V
are constructors for the datatype. The following would be valid
values for the exp datatype:

val x1 = N 0
val x2 = N 99
val x3 = V "Hello"
val x4 = V "World"

The type of state is a function that takes a string and returns an
integer. Probably is a function that ccnverts a string (such as
"123") into an integer (123).

The function expSem is a curried function that takes types of exp
followed by a state function. The function uses pattern matching to
decide which code to run. The first pattern matches a datatype with
an N constructor. The second pattern matches a datatype with a V
constructor.

The function is going to return an integer. That integer is either
directly taken from the value in the N construction. Or it's going to
convert it to an integer using the value in the V construction by
converting the string to an integer using the supplied function in the
second parameter.

Chris
Post by stainboy
Hello. I'm trying to learn something but i have big problems. Could
type var = string;
datatype exp =
N of int
| V of var;
type value = int;
type state = var -> value;
fun expSem
(N i)(s) = i
| expSem (V v)(s:state) = s v;
what mean: (N i)(s) = i
(V v)(s:state) = s v;
Thank you verry much for help.
d***@eecs.wsu.edu
2007-10-01 19:45:39 UTC
Permalink
On Sep 26, 1:00 pm, stainboy <***@gmail.com> wrote:
...
Post by stainboy
type var = string;
datatype exp =
N of int
| V of var;
type value = int;
type state = var -> value;
fun expSem
(N i)(s) = i
| expSem (V v)(s:state) = s v;
what mean: (N i)(s) = i
(V v)(s:state) = s v;
First, lets clean up a little:

fun expSem (N i)(s) = i
| expSem (V v)(s:state) = s v;

so the full expression to be evaluated is

expSem (N i)(s)

for the N datatype constructor and

expSem (V v)(s:state)

for the V datatype constructor.
Ivan Jager
2007-10-15 23:03:10 UTC
Permalink
Post by stainboy
Hello. I'm trying to learn something but i have big problems. Could
type var = string;
datatype exp =
N of int
| V of var;
type value = int;
type state = var -> value;
fun expSem (N i)(s) = i
| expSem (V v)(s:state) = s v;
what mean: (N i)(s) = i
(V v)(s:state) = s v;
It is pattern matching on the arguments of the function. Translating it
to SML syntax you may be a bit more familiar with, it is:

fun expSem2 e s =
case e of
N i => i
| V v => s v

Hope that helps.

Ivan

Loading...