小编Pis*_*tor的帖子

Haskell 中的 scanl 如何在 Either 列表上工作 - 两种情况的比较

我正在尝试使用scanlHaskell 中的函数。我已经缩小了我的问题范围,可以在以下两种情况下描述,可以使用 Haskell 中的普通库在解释器中运行,该库包含(scanl请注意,我不一定对这里的 Monadic 值感兴趣,但只是如何用于scanl确保类型一致性):

为什么以下预先列出Right value的工作有效:

*Defs> scanl (\x acc -> x ++ acc) [] [[(Right 1)], [(Right 2)]]  
[[],[Right 1],[Right 1,Right 2]]

Run Code Online (Sandbox Code Playgroud)

当这不起作用并导致以下错误消息时:

*Defs> scanl (\x acc -> [x] ++ acc) [] [(Right 1), (Right 2)]

<interactive>:36:35: error:
    * Couldn't match expected type `[[a]]'
                  with actual type `Either a0 b0'
   ...
Run Code Online (Sandbox Code Playgroud)

haskell types functional-programming type-declaration

4
推荐指数
1
解决办法
62
查看次数

使用 &gt;&gt; 而不在 monad 中显式声明它

我正在努力擅长 Monad,并编写了以下 Monad 和函数,其中我使用了>>(in the apply-function),尽管它没有在 Monad 本身中声明。为什么可以编译,据我了解http://learnyouahaskell.com/a-fistful-of-monads#walk-the-line需要在 Monad 的实例化中声明它,就像Maybe Monad

data Value =
    NoneVal
  | TrueVal | FalseVal
  | IntVal Int
  | StringVal String
  | ListVal [Value]
  deriving (Eq, Show, Read)

data RunErr = EBadV VName | EBadF FName | EBadA String
  deriving (Eq, Show)

newtype CMonad a = CMonad {runCMonad :: Env -> (Either RunErr a, [String]) }

instance Monad CMonad where
  return a = CMonad (\_ -> (Right a, [])) …
Run Code Online (Sandbox Code Playgroud)

monads haskell types functional-programming

3
推荐指数
1
解决办法
181
查看次数

使用bind在haskell中调用自定义monad

我目前正在“接触单子”(http://learnyouahaskell.com/a-fistful-of-monads#getting-our-feet-wet-with-maybe),并且仍在为这个概念的一部分而苦苦挣扎。我理解那里提到的 Maybe monad,并且可以看到如何调用它,如下所述:

ghci> return "WHAT" :: Maybe String  
Just "WHAT"  
ghci> Just 9 >>= \x -> return (x*10)  
Just 90  
ghci> Nothing >>= \x -> return (x*10)  
Nothing  
Run Code Online (Sandbox Code Playgroud)

然而,如果我用自己的类型声明我自己的实例,我该如何调用我自己的 monad 实例而不是可能的实例,如下所示:

newtype CustomM a = CustomM {runCustomM:: a -> String}


instance Monad CustomM where
  return a = CustomM (\a -> mempty)
  m >>= f = CustomM (\a -> mempty)


instance Functor CustomM where 
  fmap = liftM 
instance Applicative CustomM where 
  pure = return; (<*>) = ap

Run Code Online (Sandbox Code Playgroud)

使用 …

console monads haskell functional-programming ghci

2
推荐指数
1
解决办法
166
查看次数

Haskell 中具有多个参数的新类型

newtypes在 Haskell 中,我尝试调用我已声明的以下两个。

为什么这有效:

newtype CharList = CharList { get2CharList :: [Char] } deriving (Eq, Show)   

ghci> CharList "some"    
CharList {get2CharList = "some"}
Run Code Online (Sandbox Code Playgroud)

当这不起作用时:

newtype Pair a b = Pair { createPair :: (a, b) } deriving (Eq, Show)

ghci> Pair 2 4
<interactive>:13:1: error:
    * Couldn't match expected type: t0 -> t
                  with actual type: Pair a0 b0
    * The function `Pair' is applied to two value arguments,
        but its type `(a0, b0) -> Pair a0 b0' has …
Run Code Online (Sandbox Code Playgroud)

console haskell types functional-programming ghci

2
推荐指数
1
解决办法
162
查看次数

Haskell readP 中的 &lt;* 有何作用?

<*我在 Haskell库中遇到过readP几次,但我还不确定它的作用,并且在库文档中找不到它。与 一样吗<++

monads parsing haskell functional-programming

2
推荐指数
1
解决办法
96
查看次数

理解 Haskell 中的 RWST

我研究了这个,试图理解几个转换器单子如何相互作用,特别是更好地理解单子lift并与单子堆叠

对于这里找到的 RWST monad (我认为这是最好的文档),它是一个堆叠的 monad,其中 Reader、Writer、State 都是一个 Monadic 层(并且按照堆叠顺序)。或者说应该如何理解呢?当我查看定义时,runRWST :: r -> s -> m (a, s, w)我将其理解为将读取器环境作为状态环境并将任何单子包装在 的m返回值周围RWS。这也意味着这个monad中只存在两层monad。即外部 monadm和包含多个 monad 的元组。

这反过来也意味着您只能使用lift一次。将读取器状态单子中的值提升到外部单子中。

从这个意义上说get, 和ask只是应用两个内部单子之一的两个函数。对于最后一点,我仍然不确定我是否理解为什么即使阅读了这篇 stackoverflow 帖子,您仍然需要一个reader和一个state -monad 。我猜读者只对只读有意义,但如果不希望这样,可以在两个单独的状态单子周围使用变压器单子吗?

一个例子:

这些评论让我有理由思考并使以下内容更加明确......以下类型定义的内部单子和外部单子是什么?它本身是RWST一个包裹着(因此是外部单子)Either String(内部单子)的单子吗?

type MyRWST a = RWST
                 (String -> Either String …
Run Code Online (Sandbox Code Playgroud)

monads haskell functional-programming state-monad monad-transformers

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

数据类型已声明 - haskell 中的 newtype

我很难理解该newtype声明。我正在尝试 LYAH 中的练习:http://learnyouahaskell.com/functors-applicative-functors-and-monoids#the-newtype-keyword

并希望使用newtype来进行自定义类型实例化,Tofu Frank如下所示,遵循 LYAH 的配方:http://learnyouahaskell.com/making-our-own-types-and-typeclasses,但 newtype我自己添加了 -part 。

class Tofu t where
   tofu :: j a -> t a j

instance Tofu Frank where
   tofu x = Frank x

data Frank a b = Frank {frankField :: b a} deriving (Show)

newtype Frank a b = Frank{frankField :: (b a)} deriving Show

OUTPUT:

Multiple declarations of `Frank'
    Declared at: tryouts.hs:563:1   
                 tryouts.hs:572:1 

Run Code Online (Sandbox Code Playgroud)

在 LYAH 的描述中newtypehttp://learnyouahaskell.com/functors-applicative-functors-and-monoids#the-newtype-keyword指出: …

haskell functional-programming newtype

0
推荐指数
1
解决办法
165
查看次数

Haskells 绑定运算符和 &gt;&gt; 运算符及其关系

我最近发布了一个关于>>操作员的问题,因为尽管我已经阅读了 LYAH walk the linee 部分,但我的理解仍然存在一些差距。下面是我偶然发现的一些代码/MVE,因为它们引发了以下思考。我怎么能得到代码后面的输出?是否不会出现没有向绑定运算符提供参数的情况,因此无法str连接任何参数,否则它们会使用>>=定义中所示的绑定,并且结果不应该与预期的不相似结果如下:

import Control.Monad
import Data.List

data Value =
    NoneVal
  | IntVal Int
  | ListVal [Value]
  deriving (Eq, Show, Read)

data RErr = EBVar String  | EBInt Int
  deriving (Eq, Show)

newtype Compuptaton a = Computation {runComputation :: [(String, Value)] -> (Either RErr a, [String]) }

instance Monad Computation where
  return a = Computation( \_ -> (Right a, []))
  m >>= f = Computation(\env -> case runComputation m env of …
Run Code Online (Sandbox Code Playgroud)

monads haskell functional-programming side-effects

0
推荐指数
1
解决办法
144
查看次数