小编Fed*_*ady的帖子

如何编写“两次”以便它可以接受“交换”而不限制其类型

如果我有这些定义

twice f = f . f

swap (x,y) = (y,x)
Run Code Online (Sandbox Code Playgroud)

times 的类型被推断为(a -> a) -> a -> a,swap 被推断为(a,b) -> (b,a)

如果我写swap . swap该表达式的类型是(a, b) -> (a, b).

但如果我要求twice swap它的类型是(a, a) -> (a, a).

我知道这twice是限制 的类型swap。但我想知道是否有一种方法twice可以使其接受swap而不限制其类型。也就是说,您可以编写twice swap和接受每个组件具有不同类型的对。

同样的情况也发生在flip :: (a -> b -> c) -> b -> a -> c, 因为twice flip :: (a …

haskell type-theory

26
推荐指数
2
解决办法
1481
查看次数

这种使用函数实现数据结构的用途是什么?

我正在使用类型的函数实现一个 Seta -> Bool

到目前为止,这是我的代码:

data Set a = S (a -> Bool)

empty :: Set a
empty = S (const False)

singleton :: Eq a => a -> Set a
singleton e = S (e ==)

belongs :: Set a -> a -> Bool
belongs (S s) = s

-- private
_combine :: (Bool -> Bool -> Bool) -> (a -> Bool) -> (a -> Bool) -> (a -> Bool)
_combine op f g = \x -> f …
Run Code Online (Sandbox Code Playgroud)

haskell

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

标签 统计

haskell ×2

type-theory ×1