1

假设我们有这样的数据类型:

data NodeProperties = 
    NodeProperties
    { 
        p1 :: PropertyInfo Bool,
        p2 :: PropertyInfo [String],
        p3 :: PropertyInfo [String],
        p4 :: PropertyInfo Bool,
        p5 :: PropertyInfo [String]
    } 
    deriving (Show, Data, Typeable)

我们有一个具有这种子句的修改函数:

| p1Prefix /= Nothing =
    let 
        value = readBoolean p1Prefix
        newProperties = 
            properties
            { 
                p1 = (p1 properties){value = Just value}
            }
    in 
        modifyPropertiesNode pragmas newProperties
| p2Prefix /= Nothing =
    let 
        value = readStringList p2Prefix
        newProperties = 
            properties
            { 
                p2 = (p2 properties){value = Just value}
            }
    in 
        modifyPropertiesNode pragmas newProperties

在我看来,将上面的代码概括为类似于下面的代码会更好。

| p1Prefix /= Nothing =
    updateProperty p1 readBoolean p1Prefix pragmas
| p2Prefix /= Nothing =
    updateProperty p2 readStringList p2Prefix pragmas

为了做这样的事情,我需要在记录分配的左侧使用变量,而且 Haskell 似乎不喜欢这个想法。有没有其他方法可以实现这一目标?

4

0 回答 0