Haskell:使用|
在浏览持久性源代码时,我在下面的文件中遇到了这个函数(我引用了一个标记,其相关代码与master branch中处于当前状态的代码相同,因为该标记的代码不太可能更改)。 在行Haskell:使用|,haskell,pattern-matching,Haskell,Pattern Matching,在浏览持久性源代码时,我在下面的文件中遇到了这个函数(我引用了一个标记,其相关代码与master branch中处于当前状态的代码相同,因为该标记的代码不太可能更改)。 在行takeConstraint ps tableName defs(n:rest)| not(T.null n)&&isUpper(T.head n)=takeConstraint'中,参数模式后面有一个管道(|)字符。 |和=之间的表达式是否类似于模式中参数的约束?那么,我是否将这个|解释为数学中的同一符号,即“如此” ta
takeConstraint ps tableName defs(n:rest)| not(T.null n)&&isUpper(T.head n)=takeConstraint'
中,参数模式后面有一个管道(|)字符。
|
和=
之间的表达式是否类似于模式中参数的约束?那么,我是否将这个|
解释为数学中的同一符号,即“如此”
takeConstraint::PersistSettings
->正文
->[FieldDef]
->[正文]
->(可能是FieldDef,可能是CompositeDef,可能是UniqueDef,可能是UnboundForeignDef)
takeConstraint ps tableName defs(n:rest)| not(T.null n)&&isUpper(T.head n)=takeConstraint'--是的,它的确切意思是“这样”。这些在Haskell中非常常见(通常优先于等效的if
then
else
表达式)
case
对我来说也更合适。我同意,在防护装置有点误导之后,在两者之间塞满了整个功能块,我认为这是Haskell的一个全新功能。
takeConstraint :: PersistSettings
-> Text
-> [FieldDef]
-> [Text]
-> (Maybe FieldDef, Maybe CompositeDef, Maybe UniqueDef, Maybe UnboundForeignDef)
takeConstraint ps tableName defs (n:rest) | not (T.null n) && isUpper (T.head n) = takeConstraint' --- <<<<< This line
where
takeConstraint'
| n == "Unique" = (Nothing, Nothing, Just $ takeUniq ps tableName defs rest, Nothing)
| n == "Foreign" = (Nothing, Nothing, Nothing, Just $ takeForeign ps tableName defs rest)
| n == "Primary" = (Nothing, Just $ takeComposite defs rest, Nothing, Nothing)
| n == "Id" = (Just $ takeId ps tableName (n:rest), Nothing, Nothing, Nothing)
| otherwise = (Nothing, Nothing, Just $ takeUniq ps "" defs (n:rest), Nothing) -- retain compatibility with original unique constraint
takeConstraint _ _ _ _ = (Nothing, Nothing, Nothing, Nothing)
f x
| x > 2 = a
| x < -4 = b
| otherwise = x
f x = if x > 2 then a
else if x < -4 then b
else x
takeConstraint' = case n of
"Unique" -> (Nothing, Nothing, Just $ takeUniq ps tableName defs rest, Nothing)
"Foreign" -> (Nothing, Nothing, Nothing, Just $ takeForeign ps tableName defs rest)
"Primary" -> (Nothing, Just $ takeComposite defs rest, Nothing, Nothing)
"Id" -> (Just $ takeId ps tableName (n:rest), Nothing, Nothing, Nothing)
_ -> (Nothing, Nothing, Just $ takeUniq ps "" defs (n:rest), Nothing)