by jeremiep · 13y ago · 0 comments
In Lisp: (setq a (if b c d))
Only Lisp implements them as a function (more precisely, a macro), but to the programmer there's no difference between using if and calling a function.
Of course, they don't compile into function calls in assembly code :)
http://www.lispworks.com/documentation/HyperSpec/Body/03_aba...
IF can't be implemented as a function because of evaluation rules.
I still thought you could pass macros around like normal functions in Common-Lisp. The Lisp interpreter I wrote for fun could do that since the function value is resolved before arguments are evaluated for late macro expansions.
def iff(cond, x, y)
if cond then
x
else
y
end
end
puts iff(true, 1, puts('2'))
echoes 2
1
if may behave as a function, but you can't write a function that acts like if.edit: R does have real lazy evalution, e.g.
> iff <- function(cond, x, y) { if (cond) x else y }
> iff(TRUE, 1, cat('foo'))
[1] 1 if x then y else z
is sugar for: case x of
True -> y
False -> z
There have been suggestions to replace this sugaring with a proper "if" function, e.g.: if :: Bool -> a -> a -> a
if True x _ = x
if False _ y = y
to remove arguably redundant syntax, but it seems unlikely that this change would happen. See more here: http://www.haskell.org/haskellwiki/If-then-elseYou can write: a = b ? c : d; in C/C++, you can't write a = if(b) { c; } else { d; }.