Re: [Haskell-cafe] Why does Haskell have the if-then-else syntax?
Quoth "Sebastian Sylvan" <sylvan@student.chalmers.se>: | On 7/27/06, mvanier <mvanier@cs.caltech.edu> wrote: | > As opposed to what? | | For example case-of, guards (in combination with let or where), or | just a function: | | if :: Bool -> a -> a -> a | if True t _ = t | if False _ e = e | | -- example usage | myAbs x = if (x < 0) (negate x) x That looks to me like a different way to spell if then else, but maybe that's the answer to the question - conceptually, for every "then" there really is an "else", however you spell it, and only in a procedural language does it make any sense to leave it implicit. The exception that proves the rule is "else return ()" -, e.g., if_ :: Bool -> IO () -> IO () if_ True f = f if_ False _ = return () main = do args <- getArgs if_ (length args > 0) (print args) Strictly speaking that generalizes to any functional context where a generic value can be assigned to the else clause, but there don't tend to be that many other such contexts. Does that answer the question? Donn Cave, donn@drizzle.com
On 7/26/06, Donn Cave <donn@drizzle.com> wrote:
That looks to me like a different way to spell if then else, but maybe that's the answer to the question - conceptually, for every "then" there really is an "else", however you spell it, and only in a procedural language does it make any sense to leave it implicit. The exception that proves the rule is "else return ()" -, e.g.,
...
Strictly speaking that generalizes to any functional context where a generic value can be assigned to the else clause, but there don't tend to be that many other such contexts. Does that answer the question?
I believe his question was why if-then-else is syntax, rather than the function he gave. Since haskell is non-strict, it doesn't need to be implemented as syntax (unlike, say, scheme, where it needs to be a special form/macro to avoid executing both branches). I imagine the answer is that having the syntax for it looks nicer/is clearer. "if a b c" could be more cryptic than "if a then b else c" for some values of a, b and c. -- Dan
On 7/27/06, Dan Doel <dan.doel@gmail.com> wrote:
On 7/26/06, Donn Cave <donn@drizzle.com> wrote:
That looks to me like a different way to spell if then else, but maybe that's the answer to the question - conceptually, for every "then" there really is an "else", however you spell it, and only in a procedural language does it make any sense to leave it implicit. The exception that proves the rule is "else return ()" -, e.g.,
...
Strictly speaking that generalizes to any functional context where a generic value can be assigned to the else clause, but there don't tend to be that many other such contexts. Does that answer the question?
I believe his question was why if-then-else is syntax, rather than the function he gave. Since haskell is non-strict, it doesn't need to be implemented as syntax (unlike, say, scheme, where it needs to be a special form/macro to avoid executing both branches).
I imagine the answer is that having the syntax for it looks nicer/is clearer. "if a b c" could be more cryptic than "if a then b else c" for some values of a, b and c.
Also, you get less parenthesis: myAbs x = if x < 0 then negate x else x /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
participants (3)
-
Dan Doel -
Donn Cave -
Sebastian Sylvan