[英]Polymorphic type aliases in haskell
簡短版本:我想為一種類型的函數type myType = [Int] -> (Tree Int,[Int])
別名type myType = [Int] -> (Tree Int,[Int])
但是多態(意思是我可以放入任何東西而不僅僅是Int
s)。 我怎么能這樣做?
長版:我目前有:
data Colour = R | B deriving (Show, Read, Eq)
data Tree elt = E | T Colour (Tree elt) elt (Tree elt) deriving (Show, Read, Eq)
type Set a = Tree a
我想:
type Funcs = [elt] -> (Tree elt, [elt])
然后編寫一些具有Funcs
類型的Funcs
treeify_zero :: Treeify_t
treeify_zero lst = (E,lst)
treeify_one :: Treeify_t
treeify_one (h:t) = ((T R E h E), t)
像這樣。 目前,我無法獲得type Funcs = forall elt. [elt] -> (Tree elt, [elt])
type Funcs = forall elt. [elt] -> (Tree elt, [elt])
在GHCi中正常運行。 如果我使用type Funcs elt = [elt] -> (Tree elt, [elt])
GHCi抱怨我對treeify_zero/one
定義“應該有1個參數,但是在'to'的類型簽名中沒有給出: :: Treeify_t“
聽起來你想要參數化類型的同義詞 - elt
不能無處不在。 也許你已經過去了
type Funcs elt = [elt] -> (Tree elt, [elt])
或者你想要一些其他的活頁夾,比如type Funcs = forall elt. [elt] -> (Tree elt, [elt])
type Funcs = forall elt. [elt] -> (Tree elt, [elt])
。 但你還沒有說出你想要做什么,所以很難說。 :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.