Fun exercise: Write a Haskell function `update :: Tree a → [a] → Maybe (Tree a)`, that updates the leaves of a Tree by those given in a list (if there are not enough, return Nothing). My attempt after CW 

pop :: Compose (State [a]) Maybe a
pop = Compose $ do
xs <- get
modify' (drop 1)
pure (listToMaybe xs)

update = evalState . getCompose . go
go (Node _ []) = pure <$> pop
go (Node x ts) = Node x <$> traverse go ts

· · Web · 2 · 0 · 0
Sign in to participate in the conversation

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!