News for hackers. Stuff that matters.
Re:Learning Haskell – A Racket programmer's foray into the l... by throwaway183839 on Wednesday March 11, 2015 @07:56PM (#9188305)
> It feels a little redundant to have to manually list out all the elements when the information is known statically, and updating the list will be necessary if the Peg type changes. In Racket, writing a macro for this would be trivial, but alas, this is Haskell. Perhaps there is a way, I’m just not aware of it.

A reasonably clean way would be to define

  data Color = Red | Green | Blue | Yellow | Orange | Purple
    deriving (Enum, Bounded, Show)
and then (e.g. in ghci) you can do

  >> [minBound .. maxBound] :: [Color]
  [Red,Green,Blue,Yellow,Orange,Purple]
I often define the useful function

  enumerate :: (Enum a, Bounded a) => [a]
  enumerate = [minBound .. maxBound]
for exactly this purpose.
Re:Learning Haskell – A Racket programmer's foray into the l... by lexi-lambda on Wednesday March 11, 2015 @08:01PM (#9188331)
Yep, I posted this on r/haskell and someone pointed this out, which is cool. The provided type for Peg didn't derive Enum or Bounded, but it's a good trick to know.
Re:Learning Haskell – A Racket programmer's foray into the l... by throwaway183839 on Wednesday March 11, 2015 @08:34PM (#9188464)
I'm kind of surprised that `enumerate` isn't defined in any standard libraries (as far as I know) since I need it pretty often.

After a while spent programming in Haskell you would probably develop your own mini-library of functions that make your life easier. Another one I often find useful is

  (.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
  (.:) f g = \x y -> f (g x y)
which e.g. allows you to define the absolute distance function

  dist :: (Num a) => a -> a -> a
  dist = abs .: (-)
A fun challenge is figuring out why the definition of (.:) is equivalent to

  (.:) = fmap . fmap

!
Re:Learning Haskell – A Racket programmer's foray into the l... by JadeNB on Wednesday March 11, 2015 @10:26PM (#9188898)
> A fun challenge is figuring out why the definition of (.:) is equivalent to

> (.:) = fmap . fmap

> !

Of course it's not really equivalent, since the most general type of the LHS is as you have said, whereas that of the RHS involves functor constraints; but inlining the definition of `fmap` for arrows gives the amusing definition:

    (.:) = (.) . (.)
I tend to figure this sort of thing out by successive eta conversion:

    \f g x y -> f (g x y)
    \f g x y -> (f . g x) y
    \f g x   -> f . g x
    \f g x   -> ((f .) . g) x
    \f g     -> (f .) . g
    \f g     -> ((f .) .) g
    \f       -> ((f .) .)
    \f       -> (.) (f .)
    \f       -> (.) ((.) f)
    \f       -> ((.) . (.)) f
                (.) . (.)
Is there a better way?
Re:Learning Haskell – A Racket programmer's foray into the l... by HanyouHottie on Wednesday March 11, 2015 @10:45PM (#9188963)
Seems like a better name for it would be

  (...) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
since

  (...) = (.) . (.)

.
Re:Learning Haskell – A Racket programmer's foray into the l... by wz1000 on Wednesday March 11, 2015 @10:28PM (#9188902)

    (.:) f g = (f .) . g
This can interestingly be generalized(in a way) to get

    (.::) :: (d -> e) -> (a -> b -> c -> d) -> a -> b -> c -> d
    (.::) f g = ((f .) .) . g
and so on.

Since fmap = (.), substitution would get your point free form.

Re:Learning Haskell – A Racket programmer's foray into the l... by cschneid on Thursday March 12, 2015 @12:28PM (#9191821)
The downside of using (.:) is that it is used heavily in other libraries (Aeson for instance) in a very different context (field accessors, similar in look to json's `:`).