Having trouble reading (interpreting) this functional Miranda code.
g = (foldr (+) 0) . (foldr ((:) . ((#) . (:[]))) [])
I know what it does
- Calculate the size of a list by taking the length via
#
- Creating a one element list containing the above length of the original input list
- Collapsing the new list using
foldr
into a single integer with the operation+0
on each element
However I am confused by the brackets and don't see where the input list is fed in. What does the rightmost []
constructor do?
Also why does this code only work via the function g, but if I call it directly it throws an error?
In short,
g
is a function that returns the length of the list.Let's break the function into some parts.
I would explain some confusing functions.
return_one
(:[])
is a function that creates single element list, and(#)
returns the length. Strictly speaking,(:[])
is(:)
which takes[]
as first argument.therefore
(:[]) "hoobar" = "hoobar":[] = ["hoobar"]
, and applying(#)
to it returns 1.change_one
It starts with empty list
[]
and traverses through the list with inserting 1 to the front.