[Haskell-beginners] Where is the accumulator in the expression foldr (<=<) return (replicate x oveKnight)? -- from LYAH example

Paul aquagnu at gmail.com
Thu Jul 26 06:14:07 UTC 2018


Hello,

`return` is the initial value. So, `replicate x oveKnignt)` is a list of 
functions. `foldr` folds them with initial value `return` with the `<=<` 
between them: functions are folding with (<=<). First folding value is 
`return` function. You can check the types with :t something in the GHCi.

Result of folding is flow of functions or long functions circuit. 
`return start` is the same as to pass `start` to this functions circuit:

    inMany x start = foldr (<=<) return (replicate x oveKnight) $ start

Idea seems, to make from [KnighPos -> [KnighPos]] functions list one 
function: KnighPos -> [KnighPos] performing those functions step by step 
(<=<) and to pass `start` to it. Due to `<=<`  joining of functions is 
not "do", but "do for each...", because <=< is in the list monad. 
Something like:

    for x in f-last start:
       for y in f-prelast x:
         ...

You can look at these types:

    :t foldr
    foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
     :t (<=<)
    (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c



26.07.2018 04:44, Olumide wrotes:
> Dear List,
>
> Chapter 13 of LYAH 
> (http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions) 
> has the following code block
>
> import Data.List
>
> inMany :: Int -> KnightPos -> [KnightPos]
> inMany x start = return start >>= foldr (<=<) return (replicate x 
> oveKnight)
>
> What I'd like to know is where the accumulator of foldr is in this 
> example.
>
> Regards,
>
> - Olumide
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180726/829717dd/attachment.html>


More information about the Beginners mailing list