There is a huge difference between an if and a function. Consider SICP where lazy evaluation is introduced (read it if you haven't). An if statement encompasses the lazy evaluation as a native concept whereas a function's arguments are always evaluated (assumption for now - please carry on reading).
So consider an if statement as a control flow structure which automatically supports lazy evaluation and leaves the return semantics up to the user. This abstraction is clean from the highest level, right down to the CPU which uses conditional branching to perform the if operation. There is a 1:1 match all the way down.
The function on the other hand does not natively support lazy evaluation and enforces the return semantics. You then have to apply a lazy evaluation mechanism over the top (another layer of abstraction!). This layer of abstraction conveniently ends up using conditional branching to perform the if operation when it gets to CPU level. There is not a 1:1 match all the way down.
Big hint there: your functional if at the end of the day is just a compiler abstraction over a state machine which uses ifs.
As for OO, the industry I think has decided who won that battle.
But alas, no religious war is required. Use what tools work for you.
"your functional if at the end of the day is just a compiler abstraction over a state machine which uses ifs"
Yes, that's the 'sad' part of it. In the end there's the Intel/AMD/ARM chip and nothing more
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; }.
In any decent language, if will be an expression, not a statement. There is no difference between if and a function.