On Haskell’s (-)s

And then I learned about Haskell’s only unary operator, the minus sign… I discovered it working through one of the book’s sections on div and quot…

> -11 `div` 3
-3
> -11 `quot` 3
-3

Wait, what? The exercises suggested I should expect -4. Oh, they had some parentheses…

> (-11) `quot` 3
-3
> (-11) `div` 3
-4

Okay, cool. But what’s really going on here?  Apparently Haskell has some syntactic sugar to make negative numbers work. It’s using the “negate” function under the hood. Hmm, what about its precedence?

> negate 11 ^ 2
121
> negate (11 ^ 2)
-121

Hmm, okay I had to looked this up, and it turns out function application precedence is the highest possible. So in the first example the negate is applied to 11 first, then squared, hence positive 121.

> -11^2
-121

Huh? I thought the unary operator was syntactic sugar for negate… a function. Ack. I don’t know how to look up the “info” for the unary operator to see its precedence. I guess it’s lower than exponentiation.

Okay, so at the end of the day, -11^2 works the way it does in other languages (e.g. -11**2 in Python), so there’s no issue there, but the way that I arrived was a bit strange. Lesson learned though: parenthesize your negative numbers if that’s what you mean.

The one casualty here is sectioning with the (-) operator:

> (+3) 4
7
> (-3) 4
[... error: you tried to apply a number (negate 3) as a function...]

What if you really want to do this? Use the subtract function, for example:

> (subtract 3) 4
1

Of course that made me realize that (subtract x y) means “subtract x from y”:

> subtract 3 4
1
-- (subtract x y) == (y - x)

Fine, I’ll live with it. In any case, I can say Haskell’s positives still far outweigh its negatives.

(… Of course the next section of the Haskell Book promptly covered this. More than a little impressed that they did!)

Gory details here: https://wiki.haskell.org/Unary_operator

And here: https://www.haskell.org/onlinereport/exps.html (See section 3.4)

Advertisements

‘Let’ there be functions!

Often you hear that to declare a function in GHCi, you have to precede it with “let” – this is to “let” the interpreter know that you are giving it a declaration, rather than a statement.

Well, last night, I did the unthinkable. I forgot the leading ‘let’… gasp!

Fortunately, GHCi forgave me. It turns out you do not need ‘let’ anymore when declaring functions in GHCi. You can use it, but it’s not strictly necessary. See this page for more information: https://phabricator.haskell.org/D1299

The essence is that GHCi will try to parse the input string as a statement, and if successful, it will run it as a statement. Otherwise, it will run it as a declaration.

GHCi said, “‘Let’ there be functions!”  And so it came to be.

But Haskellers cried and gnashed their teeth, beseeching GHCi,”Oh great one, can you not tell when there be declarations, and when there be statements?”

And so GHCi realized how the parser could be used to determine the difference automatically,  and struck down this let-down! And the Haskellers agreed that it was good.

“Let” me know if the Biblical humor gets annoying… Actually don’t – I can already tell…

The Haskell Book

I’m learning Haskell, officially! I’ve made a commitment to work through the book “Haskell Programming: From First Principles” by Christopher Allen and Julie Moronuki (see here: http://haskellbook.com/) before attending LambdaConf in May of this year (2017).

Therefore, the first few posts I will make here will be self-serving – perhaps regarding surprising things I learn about Haskell and/or GHC as I work through the book’s chapters and exercises. For your reference, I am using Stack (see https://www.stackage.org/) and GHCi version 8.0.1. Disclaimer: I am not a Haskell expert by any stretch of that term. Okay?

So, without further ado…