Skip to content

§8.3.3 Exercises asks me to implement ForM using for … in in the wrong monad #274

@lilyball

Description

@lilyball

Please quote the text that is incorrect:

Reimplement Array.map, Array.find, and the ForM instance using for ... in ... loops in the identity monad and compare the resulting code.

In what way is this incorrect?

The ForM instance should use the for … in … syntax in the m monad rather than in the identity monad. Trying to do it in the identity monad is weird and AFAICT requires doing something like storing all of the monadic actions in a List and then consuming the list. Also just trying to write a ForM instance using for … in … is a bit weird to begin with, if I don't want to rely on the fact that Array already has one then I have to iterate over indices. So the identity monad version I ended up with the following:

instance [Monad m] : ForM m (Array α) α where
  forM arr f := Id.run do
    let mut xs := []
    for h : i in [0:arr.size] do
      xs := f arr[i] :: xs
    pure (xs.foldr (·*>·) (pure ()))

But if I drop the requirement that this be done in the identity monad then I get this much simpler version (once again still trying to avoid relying on the preexisting ForM impl):

instance [Monad m] : ForM m (Array α) α where
  forM arr f := do
    for h : i in [0:arr.size] do
      f arr[i]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions