| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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. |
|
#2
| |||
| |||
| 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. |
|
#3
| |||
| |||
| 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. |
|
#4
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.