Using return vs. not using return in the list monad

后端 未结 6 668
Happy的楠姐
Happy的楠姐 2020-11-27 05:50

I started my Grand Haskell Crusade (GHC :) ) and I am a bit confused with monads and IO functions. Could anyone explain simply what is the difference between those

6条回答
  •  野性不改
    2020-11-27 06:44

    My understanding is that doing

    return [1,2] when in the List monad is the same as doing

    func :: Maybe (Maybe Int)
    func = return $ Just 1
    

    which is why you end up with wrapped lists, as [] are just syntactic sugar right?

    When really he wanted to do

    func :: Maybe Int
    func = return 5
    

    or

    func = Just 5
    

    Its a bit easier to see whats going on with the Maybe monad I think.

    So when you do

    return [1,2]
    

    You are doing the same as

    [ [1,2] ]
    

提交回复
热议问题