Nav*_*ari -2 haskell types list set
我是一名初级程序员,我为我的集合创建了一个数据类型。然后我编写了一个函数将它们转换为列表,但我不断收到表达式的以下错误消息:
toList 3 :-: 4 :-: 5 :-: Empty
Run Code Online (Sandbox Code Playgroud)
toList 3 :-: 4 :-: 5 :-: Empty
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
infixr 5 :-:
data Set a = Empty | a :-: (Set a) deriving (Show, Read, Eq, Ord)
toList :: Set a -> [a]
toList Empty = []
toList (x :-: xs) = x : toList xs
Run Code Online (Sandbox Code Playgroud)
问题是它toList 3 :-: 4 :-: 5 :-: Empty被解析为(toList 3) :-: 4 :-: 5 :-: Empty,但你实际上想要toList (3 :-: 4 :-: 5 :-: Empty). 要使其工作,您需要编写后者,或使用$,如下所示:toList $ 3 :-: 4 :-: 5 :-: Empty。