Short-circuit evaluation: Difference between revisions

Content added Content deleted
(→‎{{header|Icon}}: better example)
Line 126: Line 126:
==={{header|Icon}}===
==={{header|Icon}}===
<lang Icon>procedure main()
<lang Icon>procedure main()
&trace := -1 # ensures a and b print their names


&trace := -1 # ensures functions print their names
every (i := &null | 1 ) & ( j := &null | 1) do {

every (i := false | true ) & ( j := false | true) do {
write("i,j := ",image(i),", ",image(j))
write("i,j := ",image(i),", ",image(j))
write("a & b:")
write("i & j:")
x := a(i) & b(j)
x := i() & j()
write("a | b:")
write("i | j:")
y := a(i) | b(j)
y := i() | j()
}
}

end
end


procedure true()
procedure a(x) #: returns x if x is non-null or fails otherwise
return \x
return
end
end

procedure false()
fail # for clarity but not needed as running into end has the same effect
end
</lang>
Sample output for a single case:<pre>i,j := procedure true, procedure false
i & j:
rtcircuit.icn: 8 | true()
rtcircuit.icn: 16 | true returned &null
rtcircuit.icn: 8 | false()
rtcircuit.icn: 20 | false failed
i | j:
rtcircuit.icn: 10 | true()
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}}===
==={{header|Unicon}}===
The Icon solution works in Unicon.
The Icon solution works in Unicon.