0

I am trying to write a function in CPN ML which changes 3 variables, but I don't know how, I just can write one statement. My function should be something like this:

fun T1() =
x=x+1;
y=y+2;
k=k-1;

when I write this lines of code, I get an error.

Simon Verhoeven
  • 1,295
  • 11
  • 23
sonya kochooloo
  • 114
  • 1
  • 5

2 Answers2

0

Caveat: I don't know anything about CPN ML, but based on this I guess it has syntax similar to Standard ML?

In that case, you would need to group the statements in parentheses:

fun T1 () =
  (x=x+1;
   y=y+2;
   k=k-1)
okonomichiyaki
  • 8,355
  • 39
  • 51
0

In SML, expressions can also be separated by semicolons in the body of a let expression, like this:

fun T1() =
  let in
    x=x+1;
    y=y+2;
    k=k-1
  end

Some people prefer this to parentheses because it looks more block-structured. It also gives you a place to insert declarations (in the let .. in part), which is a common way for a function to evolve.

Of course, since this is a functional language, you either need to be using reference cells (x := !x + 1) or declaring new variables (val x = x + 1) to do what you have in the body of your function. There aren't really "statements" like in C and all variables are immutable.

Tom 7
  • 507
  • 3
  • 10