Short-circuit evaluation: Difference between revisions

Content added Content deleted
(→‎{{header|Icon}}: better example)
Line 126:
==={{header|Icon}}===
<lang Icon>procedure main()
&trace := -1 # ensures a and b print their names
 
&trace := -1 # ensures a and bfunctions print their names
every (i := &null | 1 ) & ( j := &null | 1) do {
 
every (i := &nullfalse | 1true ) & ( j := &nullfalse | 1true) do {
write("i,j := ",image(i),", ",image(j))
write("ai & bj:")
x := a(i() & b(j()
write("ai | bj:")
y := a(i() | b(j()
}
 
end
 
procedure btrue(x)
procedure a(x) #: returns x if x is non-null or fails otherwise
return \x
end
 
procedure false()
fail # for clarity but not needed as running into end has the same effect
end
end</lang>
Sample output for a single case:<pre>i,j := &nullprocedure true, 1procedure false
ai & bj:
shortcicuitrtcircuit.icn: 12 8 | btrue(1)
shortcicuitrtcircuit.icn: 2316 | btrue returned 1</pre>&null
rtcircuit.icn: 8 | false()
shortcicuitrtcircuit.icn: 1920 | afalse failed
ai | bj:
shortcicuitrtcircuit.icn: 10 | atrue(&null)
rtcircuit.icn: 16 | true returned &null
i,j := procedure true, procedure true</pre>
 
procedure b(x)
return \x
end</lang>
Sample output for a single case:<pre>i,j := &null, 1
a & b:
shortcicuit.icn: 10 | a(&null)
shortcicuit.icn: 19 | a failed
a | b:
shortcicuit.icn: 12 | a(&null)
shortcicuit.icn: 19 | a failed
shortcicuit.icn: 12 | b(1)
shortcicuit.icn: 23 | b returned 1</pre>
==={{header|Unicon}}===
The Icon solution works in Unicon.