News for hackers. Stuff that matters.
Re:False Cognates and Syntax: The Importance of Syntax in Pr... by Natsu on Tuesday May 29, 2012 @07:27AM (#4036928)
For anyone who didn't know what if expressions look like in Erlang and had to look it up, apparently they use a series of "guard sequences" and the first one to be true determines the return value. If none of the sequences is true, it creates an error. There's no "else" keyword, so you have to use true as a substitute for it like this:

  is_greater_than(X, Y) ->
    if
        X>Y ->
            true;
        true -> % works as an 'else' branch
            false
    end
(Code from http://www.erlang.org/doc/reference_manual/expressions.html section 7.7)

See also: http://erlang.org/pipermail/erlang-questions/2009-January/04...

Re:False Cognates and Syntax: The Importance of Syntax in Pr... by emidln on Tuesday May 29, 2012 @08:13AM (#4037063)
This seems like the same thing that lisp has had since forever in cond. In CL:

(defun is-greater-than (x y) (cond ((< x y) T) (T nil)))

As with the erlang example, I need a T test to get an else clause, although in this particular example, the result of the cond is nil if nothing matches anyway.

Re:False Cognates and Syntax: The Importance of Syntax in Pr... by babarock on Tuesday May 29, 2012 @07:45AM (#4036983)
You can do something similar in any language that support booleans as keys for dictionaries/hashtables/maps/associative arrays/...

For instance in Lua:

    function abs(x)
        test = {
           [true]=-x,
           [x>0]=x,
           [x==0]=0}
        return test[true]
    end

    > print (abs(-3))
    3
To understand this code, think that `test[true]` is overwritten at initialization time by the last predicate to evaluate to True.

It's not the best way to do this, but maybe it'will help understand the above construct.

Re:False Cognates and Syntax: The Importance of Syntax in Pr... by reycharles on Tuesday May 29, 2012 @07:57AM (#4037025)
There's a difference, though. This example evaluates every "branch".
Re:False Cognates and Syntax: The Importance of Syntax in Pr... by reikonomusha on Tuesday May 29, 2012 @10:14AM (#4037540)
Make it even more ridiculous and wrap every branch in a lambda function, then do

    test[true]()
Re:False Cognates and Syntax: The Importance of Syntax in Pr... by mahcuz on Tuesday May 29, 2012 @10:35AM (#4037629)
That would only evaluate the original [true] key, though, right?
Re:False Cognates and Syntax: The Importance of Syntax in Pr... by tgasson on Tuesday May 29, 2012 @10:51AM (#4037711)
No, `test[true]` will be reassigned to the given function each time the condition evaluates to true. Calling `test[true]()` will execute that given function.
Re:False Cognates and Syntax: The Importance of Syntax in Pr... by tinco on Tuesday May 29, 2012 @07:39AM (#4036967)
Haskell has the exact same thing. It defines an alias for true called otherwise, so it would be

if x > y -> true; otherwise -> false; end

Using otherwise makes it a loss less ugly, and I guess fixes the false cognate.

Re:False Cognates and Syntax: The Importance of Syntax in Pr... by Tyr42 on Tuesday May 29, 2012 @11:50AM (#4038027)
I don't think that's valid Haskell. Examples of Haskell:

    case (x>y) of {True -> "Yup"; False -> "Nope");}
or

    if (x>y) then "Yup" else "Nope"
An example that uses otherwise would be

    fizzbuzz x | x `mod` 15 == 0 = "Fizzbuzz"
               | x `mod` 5  == 0 = "Buzz"
               | x `mod` 3  == 0 = "Fizz"
               | otherwise       = show x  -- This is x.to_string