Ben373
Ben373

Reputation: 971

Haskell foldl Monad bind

I got a function

move :: Move -> Node -> Maybe Node

where I can use my bind monad to obtain a Maybe Node

(return n >>= move m) 

where n::Node and m::Move, but how I can now fold through a list of Moves ([Move])?

I tried to do it with foldl but without success.

Upvotes: 1

Views: 976

Answers (1)

Lee
Lee

Reputation: 144196

If you have an initial Node and a [Move] you can use foldM:

moveAll :: Node -> [Move] -> Maybe Node
moveAll startNode moves = foldM (\n m -> move m n) startNode moves

or simply

moveAll = foldM (flip move)

Upvotes: 5

Related Questions