Unwrapping datatypes in Haskell without extraneous code

前端 未结 3 1353
臣服心动
臣服心动 2021-01-11 12:21

Say I have

x = Just 2

Is there a way (preferrably a builtin mechanism/function) to use x in a single statement such that if it is a Just, t

3条回答
  •  长发绾君心
    2021-01-11 13:18

    For this particular case, fromJust. In general

    let Just k = x in f k + 2 == 4
    

    This trick works with any datatype constructor and is very commonly used with (:) for nonempty lists.

提交回复
热议问题