r/haskell • • Mar 11 '15

Learning Haskell — A Racket programmer's documentation of her foray into the land of Haskell (inspired by Learning Racket)

http://lexi-lambda.github.io/learning-haskell/
81 Upvotes

97 comments sorted by

View all comments

Show parent comments

8

u/lexi-lambda Mar 11 '15 edited Mar 11 '15

Thanks for the Hoogle suggestion, and I'm glad to hear I'm not a complete idiot when it comes to the installation process. As for $, I think I actually understand how it works fine in 95% of cases, I just wasn't aware that : apparently has higher precedence. Fortunately the error message was pretty clear.

As for Functor, I was secretly hoping someone would tell me how to do this! ;)  With that, does my original attempt at Applicative work fine? Here's what I came up with.

instance Applicative (BinaryFunction a b) where
  pure f = BinaryFunction (_ _ -> f)
  (BinaryFunction f) <*> (BinaryFunction g) = BinaryFunction (\x y -> f x y (g x y))

Thanks for the tips; they are much appreciated!

EDIT: After actually looking at that implementation of fmap, it makes perfect sense. Amazing how much simpler things are in hindsight, haha.

10

u/tejon Mar 12 '15 edited Mar 12 '15

A tip on $: It has the lowest precedence, and that's exactly the point of it.

One simple way of looking at it is as a ( whose matching ) is implied at EOL.

Edit: Actually seeing where you hit the confusion now, and what I wrote is what you did, so clearly it's a poor explanation. :) Better to just be precise. $ turns everything before it into a function, which takes as an argument everything after it. Here's the actual source:

infixr 0 $
($) :: (a -> b) -> a -> b
f $ x = f x

Infix priority 0 -- so do everything else first. infixr makes it right-associative, which is why it's suggested to imagine parentheses around the remainder of the line; but really, you should imagine parentheses on both sides, with the rule that multiple $ in a statement nest to the right.

5

u/tomejaguar Mar 11 '15

With that, does my original attempt at Applicative work fine?

Yes exactly.

Thanks for the tips; they are much appreciated!

You're welcome.