SML please for translation

This is a discussion on SML please for translation within the ml forums in Programming Languages category; 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....

Go Back   Application Development Forum > Programming Languages > ml

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-26-2007, 04:00 PM
stainboy
Guest
 
Default SML please for translation

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.



Reply With Quote
  #2  
Old 10-01-2007, 03:45 PM
Chris Rathman
Guest
 
Default Re: SML please for translation

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

On Sep 26, 3:00 pm, stainboy <jakub.p...@gmail.com> wrote:
> 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.





Reply With Quote
  #3  
Old 10-01-2007, 03:45 PM
dbenson@eecs.wsu.edu
Guest
 
Default Re: SML please for translation

On Sep 26, 1:00 pm, stainboy <jakub.p...@gmail.com> wrote:
....
>
> 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;
>

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.



Reply With Quote
  #4  
Old 10-15-2007, 07:03 PM
Ivan Jager
Guest
 
Default Re: SML please for translation

On 2007-09-26, stainboy <jakub.pola@gmail.com> wrote:
> 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;


Formatting this a bit nicer:
> 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;


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


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:35 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.