[Haskell-beginners] foldr with ($) as the passed function

Lawrence Bottorff borgauf at gmail.com
Thu Jan 14 06:24:26 UTC 2021


With 99 questions <https://wiki.haskell.org/99_questions/Solutions/3> Problem
3 wants a function elementAt that will take a list and an index and return
the element for that index. One very odd version in the solutions is

elementAt xs n = head $ foldr ($) xs $ replicate (n - 1) tail

So the function "passed" is ($) and the accumulator "seed" is the incoming
list xs and the list to be worked on is (replicate (n-1) tail) which . . .
and I can't fathom what's happening -- other than perhaps (replicate (n-1)
(tail xs))

elementAt [1,2] 2 would be

foldr ($) [1,2] (replicate 1 (tail [1,2])
foldr ($) [1,2] ([2])

. . . now I'm lost. Can someone walk me through this?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210114/8a4621cf/attachment.html>


More information about the Beginners mailing list