小编ter*_*ero的帖子

Haskell:如何为集合定义类型类?

在这里总新手,苦苦挣扎.

我正在尝试为集合定义类型类.对于这种情况,它只需要'exists'的定义.'exists'将在set项上设置一个set和function,并返回一个布尔值.我怎样才能在Haskell中定义它?

即使是在正确的方向,以下是什么?所以有类型类定义和set with list的实现,'exists'现在返回true.

-- Set.hs --

class Set a b where

  exists :: a -> (b -> Bool) -> Bool


-- ListSet.hs --

instance Set ListSet a where 

  exists a f = True
Run Code Online (Sandbox Code Playgroud)

-

(结果:"Set"类的参数太多)

haskell set typeclass

8
推荐指数
2
解决办法
1335
查看次数

Haskell:来自这种数据类型的仿函数?

抱歉标题不好.我有一个有问题的数据类型,我试图将其定义为仿函数的实例.

基本上,我所拥有的是具有的东西

sample_logp :: s a -> a
Run Code Online (Sandbox Code Playgroud)

它应该使用转换

(a -> b)
Run Code Online (Sandbox Code Playgroud)

sample_logp :: s b -> b
Run Code Online (Sandbox Code Playgroud)

.以下代码并没有完全实现这一点,并且仅在成功

sample_logp :: s a -> b  
Run Code Online (Sandbox Code Playgroud)

.

data Model s a = Model {
  sample_logp :: s a -> a
}

instance Functor (Model s) where
  fmap f m = Model {
    sample_logp = sample_logp'
  } where sample_logp' x = (f . (sample_logp m)) x
Run Code Online (Sandbox Code Playgroud)

我正在尝试甚至可能吗?如果是这样,如何更新此代码以实现此目的?

haskell functor

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

Haskell中的Runge-Kutta(RK4),类型系统问题

我正在尝试在Haskell中实现4阶Runge-Kutta,但我发现很难将Haskell类型系统用于此任务.有人可以帮忙吗?我希望在以下代码中将"State"和"DState"类型更改为类型类:

data State = State Double deriving (Show)
data DState = DState Double deriving (Show)

update :: State -> DState -> State
update (State x) (DState y) = State (x+y)

add :: DState -> DState -> DState
add (DState x) (DState y) = DState (x + y)

scale :: Double -> DState -> DState
scale h (DState x) = DState (h*x)


update_rk4 :: State -> (Double -> State -> DState) -> Double -> Double -> State
update_rk4 y f t …
Run Code Online (Sandbox Code Playgroud)

haskell types runge-kutta

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

标签 统计

haskell ×3

functor ×1

runge-kutta ×1

set ×1

typeclass ×1

types ×1