Live data from Hacker News

How I Start: Haskell

howistart.org

1–10 of 61 posts

Re: How I Start: Haskell

#2
What do you think about the lens library?

I have been trying Haskell for a few weeks and this library feels strange. Also I don't really think I need the things it provides. But perhaps I just don't understand it.

Re: How I Start: Haskell

#3
Nicely done. I find that the most useful pedagogic tool for learning are extensively annotated non-trivial examples (with emphasis on extensively annotated).

Re: How I Start: Haskell

#4
post #2

What do you think about the lens library? I have been trying Haskell for a few weeks and this library feels strange. Also I don't really think I need the things it provides. But perhaps I just don't understand it.

It's a pretty advanced library, and I don't think you should use it if you've only been trying Haskell for a few weeks. I've been using Haskell for about 7 years, 5 of which commerically, and I've never used it seriously. I find the smaller lens libraries, like 'fclabels', solve the most important problems 'lens' also solves, but with a lot less surface area and complexity. But even that can wait until you're further along.

Re: How I Start: Haskell

#6
post #2

What do you think about the lens library? I have been trying Haskell for a few weeks and this library feels strange. Also I don't really think I need the things it provides. But perhaps I just don't understand it.

For me it greatly simplifies my data access code. Sometimes I'll have multiple-layer nested data objects, and it's just not reasonable to access that any other way.

It's useful, but the amount of magic in it can be off-putting. For average developers it's a tradeoff of magic for functionality. Debugging it can be a nightmare if you use too much of the magic, but using it for simple data access and setting can be pretty straightforward.

Re: How I Start: Haskell

#7
post #2

What do you think about the lens library? I have been trying Haskell for a few weeks and this library feels strange. Also I don't really think I need the things it provides. But perhaps I just don't understand it.

Lens is fantastic when you're doing a lot of work with deeply nested data. Often times, you shouldn't be doing that in the first place.

Re: How I Start: Haskell

#8
post #2

What do you think about the lens library? I have been trying Haskell for a few weeks and this library feels strange. Also I don't really think I need the things it provides. But perhaps I just don't understand it.

You can think of a lens as just a pointer (like in C). They're not exactly like a pointer...you can't get or modify the address that it points to. But you can get and modify the thing that it points to, which is primarily how you use pointers in C.

But lenses can do things that pointers can't do. Namely, they can be composed. If you have a lens that points to a person's birth date

    dob :: Lens Person Date
...and a lens that points to a date's year field:

    year :: Lens Date Int
Then you can compose them like this:

    (dob . year) :: Lens Person Int
Normally it would be easy to retrieve the person's birth year with plain function composition. Lenses let you do that too. But setting the person's birth year is much more painful. This is where lenses shine.

    set (dob . year) 2004 person

Re: How I Start: Haskell

#9
post #2

What do you think about the lens library? I have been trying Haskell for a few weeks and this library feels strange. Also I don't really think I need the things it provides. But perhaps I just don't understand it.

Lens is difficult to get your head around. It is simultaneously a rather simple idea ("bundle getters and setters together... ") and a ridiculously sophisticated and generalized implementation ("... in every conceivable lawful way such that they all work together as they ought to!").

You probably do want lenses. Learning to use them is its own "mind blowing" experience similar to learning functional programming itself. Better, they integrate smoothly with Haskell so once you've learned them the cost of using them is quite small.

Unfortunately, learning lenses from `lens` itself is quite hard. There is some amount of training materials (shameless plug here)

    https://www.fpcomplete.com/user/tel/a-little-lens-starter-tutorial
    https://www.fpcomplete.com/user/tel/lens-aeson-traversals-prisms
but the library itself is typically targeting a proficient user far better than a new one. I'd recommend trying to implement your own lenses as a learning task. If you don't bother generalizing it as far as Ed has then the core concept is very simple. I wrote up a CodeWars task for this using the Van Laarhoven style used in `lens`, but there are even simpler forms as well.

    http://www.codewars.com/kata/54258ffb430ca2e4b5000239

Re: How I Start: Haskell

#10
This is a great, well-written article. I'm going to try it out later tonight. Hopefully going through a real-life example will make LYAH a little less cryptic.
Post reply on HN