Haskell:在Getter中使用或使用 [英] Haskell: use or uses in Getter

查看:255
本文介绍了Haskell:在Getter中使用或使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Control.Lens中,我们有可以访问嵌套结构的Getter。 Getter有使用和使用,但我不清楚他们是如何工作的。所以如果有人能够提供一些使用或使用的简单示例,那将会很棒。

为什么我需要知道它?因为我正在阅读一些在Haskell中使用了实现,并在其中使用了uses和use。特别是它说:

  inRange<  - 使用fsCurrentCoinRangeUpperBound(coinIndex< =)
解决方案

正如我在回答您的其他问题时所做的那样,在用makePrisms的例子与例子有很多必要的知识要求,然后才能明白这一点。 p>

首先,你必须很好地理解Lens。从你的另一个问题来看,你刚刚开始。这很棒!它们非常酷,并且非常适合解决这些问题。



然而,我在这里给你一个很大的警告,Haskell的危险之一就是它 so 功能强大,并且可以如此表达和简洁,以至于很容易尝试跳过内容。

例如,如果您很好地忽略了对代数数据类型的理解,那么您可以轻松阅读代码, think 可以在您完全不了解的情况下理解它。这可能会导致混淆,你会觉得你不了解任何它,这实际上可能是真实的,但是这种感觉在学习Haskell时不是一种好的感觉。



我不希望你有这样的感觉。

所以我鼓励你学习Lens,但如果你没有对Lens的必备知识,那么我鼓励你先弄清楚。在某种程度上不难理解这些内容,但对于不太熟悉至少简单类型,参数化类型,代数的程序员来说,写出Lens的方式并不是微不足道或容易接近的。数据类型,类型类型,函数类型类型,并真正理解它,你需要了解Functor的几个实例。



同样,如果你试图理解 use 使用,这在处理 State 时才有意义。值,那么我建议在不知道 State 的情况下理解发生的事情几乎是不可能的,以及镜头确实是。



使用使用用于获取镜头和状态值,并查看 State 值内的当前状态。因此,在某种程度上,您还需要明白 do 语法正在做什么,因此 Monad 类型类别具有某种程度,以及 State / MonadState 如何从这个角度发挥作用。



如果跳过了这些预赛中的任何一个,你会感到困惑。



我希望这有助于!我希望你好。


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?

解决方案

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.

这篇关于Haskell:在Getter中使用或使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆