The question came up recently in class: How would we do logical AND and OR in Io? The following is an answer, but it requires that Io do Currying (we'll see Currying officially when we study ML).
1. define OR: -> cond1 cond2 Y N; 2.
cond1 Y (cond2 Y N) .
3. define AND: -> cond1 cond2 Y N; 4.
cond1 (cond2 Y N) N .
5. -- sample usage 6. OR (= 3 5) (> 7 3) (write 1; terminate) (write 0; terminate) 7.
-- expected output: 1 The parentheses are used to group tokens into a single parameter. When we invoke OR in line 6, we get the following bindings of actuals to formals: The OR function executes line 2, invoking
actual formal = 3 5 cond1 > 7 3 cond2 write 1; terminate Y write 0; terminate N
cond1on two parameters. Here is where the Currying comes in. The formal
cond1is bound to the actual
= 3 5which is a call to the
=function with two parameters. But that function expects four parameters. We have Curried two of them and left the other two for line 2 to fill in. The intended effect is that we call
=with four parameters, as if it had said= 3 5 Y (cond2 Y N)or, expanding all formal parameters to show the actuals,= 3 5 (write 1; terminate) (> 7 3 (write 1; terminate) (write 0; terminate))This call is precisely what we would want the OR to mean. AND works similarly.