36 lines
827 B
Standard ML
Executable file
36 lines
827 B
Standard ML
Executable file
#!/usr/bin/env fun
|
|
|
|
// Short-circuit demo for || and &&
|
|
|
|
print("=== short-circuit demo ===")
|
|
|
|
fun mark(name, v)
|
|
// Show when this branch is evaluated
|
|
print(name)
|
|
return v
|
|
|
|
// OR short-circuits: RHS not evaluated when LHS is true
|
|
if (true || mark("OR-RHS", 0))
|
|
print(1) // expect: prints 1; no "OR-RHS"
|
|
else
|
|
print(0)
|
|
|
|
// AND short-circuits: RHS not evaluated when LHS is false
|
|
if (false && mark("AND-RHS", 1))
|
|
print(1)
|
|
else
|
|
print(0) // expect: prints 0; no "AND-RHS"
|
|
|
|
// OR evaluates RHS when needed (LHS false)
|
|
if (false || mark("OR-NEEDED", 1))
|
|
print(1) // expect: prints "OR-NEEDED" then 1
|
|
else
|
|
print(0)
|
|
|
|
// AND evaluates RHS when needed (LHS true)
|
|
if (true && mark("AND-NEEDED", 1))
|
|
print(1) // expect: prints "AND-NEEDED" then 1
|
|
else
|
|
print(0)
|
|
|
|
print("=== end ===")
|