2017-03-27 11 views
3

私はPersistentソースコードをブラウズしていましたが、ファイルQuasi.hsの下にこの関数があります(masterブランチの現在の状態にある関連するコードを持つタグを参照しました。これはタグのコードが変更されそうにないためです)。 takeConstraint ps tableName defs (n:rest) | not (T.null n) && isUpper (T.head n) = takeConstraint'行には、引数のパターンの後にこのパイプ(|)文字があります。 |=の間の式は、パターンの引数の制約と同じですか?ですから、私はこの|を数学の同じ記号として解釈しますか?つまり、"など"ハスケル:関数関数を拘束する|

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) 

答えて

6

はい、それは正確には「ようにこと」を意味します。これらは、guardsであり、ハスケルでは非常に一般的です(一般的には同等の表現に好ましくなります)。ifthenelseの表現です。

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 

IMOその具体的な例に相当し、実際より良いifも警備員でもないと書かれていたであろうが、

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) 
+0

'case'は、同様に私のために良く見えます。 – chi

+0

私は、ガードが少し誤解を招くようになった後で、全体の機能ブロックを詰め込んで、ハスケルから全く新しい機能だと思ったことに同意します。 – Rafael