检查自由变量

时间:2012-11-09 20:44:12

标签: string variables haskell substitution

解析错误是目前我最不喜欢的Haskell错误类型。所以我正在写一个函数,我得到一个对的列表,其中第二个组件包含一些变量(由vars确定,我之前在我的代码中定义为vars :: a -> [String]。如果列表为空然后没有任何自由变量,替换被认为是解决了。

我已经这样做了,但我得到了parse error on '|'

solved :: Subst a -> Bool
solved xs = null [(S(xs,d) | (S(xs,d)) <- xs, not (null (vars d)))]

我之前将我的数据类型定义为

data Subst a = S [(String,a)]

我鄙视解析错误,因为需要永远弄清楚什么是错的(笑)。任何人的想法?

2 个答案:

答案 0 :(得分:1)

solved xs = null [(S(xs,d) | (S(xs,d)) <- xs, not (null (vars d)))]
                         ^^

缺少一个右括号。

但是,看起来你还有其他问题,

data Subst a = S [(String,a)]

solved :: Subst a -> Bool
solved xs = null [(S(xs,d) | (S(xs,d)) <- xs, not (null (vars d)))]

xs不是pattern <- xs中的列表,而是Subst a。此外,您正在隐藏名称xs,这使代码难以遵循。你可能想要像

这样的东西
solved (S xs) = null [ () | (varname, d) <- xs, not (null $ vars d) ]

答案 1 :(得分:1)

你得到了解析错误,因为|在一对括号内 - 它需要直接在列表推导的方括号内,才能在语法上有效。

修复之后,你会收到一个类型错误,因为你的类型签名表明你的函数需要Subst a,但是你把你的参数看成是一个列表。

我还怀疑你的vars函数会遇到问题,因为a -> [String]类型的函数确实不合理(其中“明智的”我的意思是'没有' t只为每个参数返回相同的字符串列表。)