如何在我的列表中实现应用程序,为非确定性建模?
How can I implement Applicative on my list, modelling non-determinism?
当我遇到 this assignment:
时,我正在 Haskell 中练习 Functor / Applicative Functors + Monad
4) You have seen how a Monad instance can be used to handle things that can
fail. Another use case for a Monad is dealing with non-determinism. The simplest
way to model non-deterministic computation is using a list. Think of the list as
holding all possible results of some computation. When you use the well known map
function, the function you are applying produces the outputs for all the possible
inputs. [...]
Define your list type like so:
data MyList a = Cons a (MyList a) | Nil deriving Show
(a) Make your list an instance of Functor.
(b) Make your list an instance of Applicative.
(c) Make your list an instance of Monad.
我遇到了一些问题。这是我目前的解决方案:
instance Functor MyList where
fmap f (Nil) = Nil
fmap f (Cons item other) = (Cons (f item) (fmap f other))
instance Applicative MyList where
pure item = (Cons item Nil)
Nil <*> _ = Nil
_ <*> Nil = Nil
(Cons f item) <*> something = (fmap f something) ++ (item <*> something)
(item <*> something)
有问题。使用列表 ++
运算符可以实现这一点,但我什至创建了一个具有 ++
运算符的函数,但此方法仍然无效。如何为这种类型实现 Applicative?
这是我的 ++ 实现:
(++) :: (MyList a) -> a -> (MyList a)
(++) Nil item = (Cons item Nil)
(++) (Cons val other) item = (Cons val (other ++ item))
这是我得到的错误:
Couldn't match expected type ‘a -> b’
with actual type ‘MyList (a -> b)’
Relevant bindings include
something :: MyList a (bound at Assignment_6.hs:13:23)
item :: MyList (a -> b) (bound at Assignment_6.hs:13:13)
f :: a -> b (bound at Assignment_6.hs:13:11)
(<*>) :: MyList (a -> b) -> MyList a -> MyList b
(bound at Assignment_6.hs:11:5)
In the second argument of ‘(++)’, namely ‘item’
In the first argument of ‘(<*>)’, namely
‘(fmap f something) ++ item’
比较以下两种类型:
(Prelude.++) :: [] a -> [] a -> [] a
(George.++) :: MyList a -> a -> MyList a
你能找出问题所在吗?
当我遇到 this assignment:
时,我正在 Haskell 中练习 Functor / Applicative Functors + Monad4) You have seen how a Monad instance can be used to handle things that can fail. Another use case for a Monad is dealing with non-determinism. The simplest way to model non-deterministic computation is using a list. Think of the list as holding all possible results of some computation. When you use the well known map function, the function you are applying produces the outputs for all the possible inputs. [...]
Define your list type like so:
data MyList a = Cons a (MyList a) | Nil deriving Show
(a) Make your list an instance of Functor.
(b) Make your list an instance of Applicative.
(c) Make your list an instance of Monad.
我遇到了一些问题。这是我目前的解决方案:
instance Functor MyList where
fmap f (Nil) = Nil
fmap f (Cons item other) = (Cons (f item) (fmap f other))
instance Applicative MyList where
pure item = (Cons item Nil)
Nil <*> _ = Nil
_ <*> Nil = Nil
(Cons f item) <*> something = (fmap f something) ++ (item <*> something)
(item <*> something)
有问题。使用列表 ++
运算符可以实现这一点,但我什至创建了一个具有 ++
运算符的函数,但此方法仍然无效。如何为这种类型实现 Applicative?
这是我的 ++ 实现:
(++) :: (MyList a) -> a -> (MyList a)
(++) Nil item = (Cons item Nil)
(++) (Cons val other) item = (Cons val (other ++ item))
这是我得到的错误:
Couldn't match expected type ‘a -> b’
with actual type ‘MyList (a -> b)’
Relevant bindings include
something :: MyList a (bound at Assignment_6.hs:13:23)
item :: MyList (a -> b) (bound at Assignment_6.hs:13:13)
f :: a -> b (bound at Assignment_6.hs:13:11)
(<*>) :: MyList (a -> b) -> MyList a -> MyList b
(bound at Assignment_6.hs:11:5)
In the second argument of ‘(++)’, namely ‘item’
In the first argument of ‘(<*>)’, namely
‘(fmap f something) ++ item’
比较以下两种类型:
(Prelude.++) :: [] a -> [] a -> [] a
(George.++) :: MyList a -> a -> MyList a
你能找出问题所在吗?