Implementing this monad/type in Haskell?

狂风中的少年 提交于 2019-12-06 02:30:40
dave4420

In terms of syntax, it's

instance Monad (DataPoint x) where
    -- etc

Although I share hammar's concerns and think you should be trying to make it a Functor instead:

instance Functor (DataPoint x) where
    -- etc

The kind error you get

The first argument of `Monad' should have kind `* -> *',
but `DataPoint x dataval' has kind `*'

is because Monad and Functor are typeclasses that apply to higher order types (compare with Monoid, a typeclass that applies to simple types).

e.g. IO Int is not a monad; IO () is not a monad; IO is a monad.


I want a type DataPoint, which stores either a tuple (x, dataval) or two fields x and dataval (where x is a Double and dataval is a Complex Double.

data DataPoint a = DataPoint {x :: Double,
                              dataval :: a}

instance Functor DataPoint where
    fmap f dataPoint = DataPoint {x = x dataPoint,
                                  dataval = f (dataval dataPoint)}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!