Use cases for functor/applicative/monad instances for functions

后端 未结 3 1294
抹茶落季
抹茶落季 2021-02-01 21:07

Haskell has Functor, Applicative and Monad instances defined for functions (specifically the partially applied type (->) a

3条回答
  •  忘掉有多难
    2021-02-01 21:23

    here an application of the bind function that I used for solving the Diamond Kata. Take a simple function that mirrors its input discarding the last element

    mirror :: [a] -> [a]
    mirror xs = xs ++ (reverse . init) xs
    

    let's rewrite it a bit

    mirror xs = (++) xs ((reverse . init) xs)
    mirror xs = flip (++) ((reverse . init) xs) xs
    mirror xs = (reverse . init >>= flip (++)) xs
    mirror = reverse . init >>= flip (++)
    

    Here is my complete implementation of this Kata: https://github.com/enolive/exercism/blob/master/haskell/diamond/src/Diamond.hs

提交回复
热议问题