相关疑难解决方法(0)

Haskell中的美元符号($)和id函数之间是否存在关系?

有一天我正在阅读关于Monad挑战的评论(我强烈推荐给Haskell的任何初学者,就像我一样),我最终在这个帖子中读到了这一点($) = id.

我不知道如何吓唬人,但许多编程语言的概念最好用小例子来说明,这些例子让人们说"哇".

例如,令人惊讶的是,Prolog中的append()可以从连接结果"向后"运行,以产生可以连接以生成它的所有列表.或者Haskell(>> =)中的monadic绑定运算符可以用join和fmap或($)= id来定义.

($) = id !?

<在Raskell/Ghci中尝试>

我现在明白为什么这是真的,但仍然......哇!感谢那!(......)

然后我检查了base-4.10.0.0代码,寻找的定义($)id,但就在我读这顶:

NOTA BENE: Do NOT use ($) anywhere in this module! The type of ($) is
slightly magical (it can return unlifted types), and it is wired in.
But, it is also *defined* in this module, with a non-magical type.
GHC gets terribly confused (and *hangs*) if you try to use ($) in this …

haskell

17
推荐指数
2
解决办法
582
查看次数

申请真正适用于申请,而不是"结合"?

对于不确定性传播Approximate类型,我想要Functor通过实例Monad.然而,这不起作用,因为我需要在包含的类型上使用向量空间结构,因此它实际上必须是类的受限版本.由于仍有似乎并不为那些标准库(或者是那里?请点我.还有rmonad,但它使用*,而不是Constraint作为上下文样,这似乎只是过时的我),我写我自己的版本的暂时的.

这一切都很容易 Functor

class CFunctor f where
  type CFunctorCtxt f a :: Constraint
  cfmap :: (CFunctorCtxt f a, CFunctorCtxt f b)  => (a -> b) -> f a -> f b

instance CFunctor Approximate where
  type CFunctorCtxt Approximate a = FScalarBasisSpace a
  f `cfmap` Approximate v us = Approximate v' us'
   where v' = f v
         us' = ...
Run Code Online (Sandbox Code Playgroud)

但是直接翻译Applicative,就像

class CFunctor …
Run Code Online (Sandbox Code Playgroud)

haskell type-constraints category-theory applicative

12
推荐指数
1
解决办法
624
查看次数