How to chain the use of maybe argument in haskell?

前端 未结 5 1751
渐次进展
渐次进展 2021-01-16 09:06

I\'m trying to build a string from optional arguments. For example to generate a greeting string from a title and a name This is trivial in a imperative language and would l

5条回答
  •  长发绾君心
    2021-01-16 09:23

    I know it's an old post ... but it may be useful ...

    in a pure "functional" way ...

    greeting :: Maybe String -> String -> String
    greeting Nothing name  = "Hello" ++ ", " ++ name
    greeting (Just title) name  = "Hello" ++ ", " ++ title ++ " " ++ name ++ "!" 
    
    how to call : 
    -- greeting Nothing "John"
    -- greeting Nothing "Charlotte"
    -- greeting (Just "Mr.") "John"
    -- greeting (Just "Miss.") "Charlotte"
    

提交回复
热议问题