How to get the value of a Maybe in Haskell

前端 未结 5 666
南旧
南旧 2021-01-31 08:15

I\'m relatively new to Haskell and began to read \"Real World Haskell\".

I Just stumbled over the type Maybe and have a question about how to receive the actual value fr

5条回答
  •  终归单人心
    2021-01-31 09:20

    I'm new to Haskell too, so I don't know if this exists in the platform yet (I'm sure it does), but how about a "get or else" function to get a value if it exists, else return a default?

    getOrElse::Maybe a -> a -> a
    getOrElse (Just v) d = v
    getOrElse Nothing d  = d
    

提交回复
热议问题