0

In Control.Lens we have Getter that can access the nested structure. Getter has use and uses, but it's not clear to me how they work. So it'd be great if someone can provide some simple examples that use or uses is utilised.

Why do I need to know it? because I'm reading some implementation in Haskell and "uses" and "use" were used in them. In particualr it says:

inRange <- uses fsCurrentCoinRangeUpperBound (coinIndex <=)

If the above code is just for comparing (<=) two values, then why do we need "uses" at all, there?

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
4xx
  • 165
  • 8

1 Answers1

0

As I tried to make clear in my answer to your other question over at Use cases of makePrisms with examples there is a lot of requisite knowledge required before you can understand this.

First up, you have to understand Lens quite well. Judging from your other question you're just beginning them. This is great! They're amazingly cool and it's excellent to tackle such things.

However, I'd give you a big amount of caution here, one of the dangers of Haskell is it's so powerful, and can be so expressive and terse, that it seems easy to try to skip stuff.

If you skipped understanding algebraic data types very well, for example, you can easily read code and think you have an understanding of it when you don't at all. This can then lead to compounded confusion, and you'll feel like you don't understand any of it, which actually might be true, but that feeling is not a good feeling to have when learning Haskell.

I don't want you to feel like that.

So I encourage you to learn Lens, but if you don't have the requisite knowledge for Lens, then I encourage you to go get that first. It's not too hard to understand this stuff to a degree, but the way Lens is written is not trivial or easy to approach for programmers who aren't quite familiar with at least simple types, parameterized types, Algebraic Data Types, Typeclasses, the Functor typeclass, and to really understand it, you need to understand several instances of Functor.

As well, if you're trying to understand use and uses, which only make sense when dealing with State values, then I'd suggest it's almost impossible to understand what's happening without knowing what State is, as well as what Lens does and is.

use and uses are for taking a lens and a state value and looking into the current state inside a State value. So, to a degree you really need to also understand what do syntax is doing, therefore the Monad typeclass to a degree, as well as how the State / MonadState work from that perspective.

If any of these preliminaries are skipped, you'll be confused.

I hope this helps! And I wish you well.

Julian Leviston
  • 1,646
  • 10
  • 21
  • thank you for the answer, I can see that you're suggesting learning Lenses first. My question is: Is there any textbook covering lenses with some examples? – 4xx Jul 09 '17 at 14:22
  • Sadly there aren't any textbooks that I know of that cover lenses yet. I'd like to add them to a future volume of my tutorial http://happylearnhaskelltutorial.com but as you can see it's much more beginner focussed one at this point. My recommendation to learn Lenses is to study the page at https://github.com/ekmett/lens and build some toy eamples of your own, and ask as many questions as you have on Stack Overflow about them. Also to really study the types, and this series, too: https://artyom.me/lens-over-tea-1 it's very good. (probably start with that first) – Julian Leviston Jul 10 '17 at 12:37