[Haskell-beginners] defining 'init' in terms of 'foldr'

dan portin danportin at gmail.com
Tue Dec 7 06:22:11 CET 2010


On Mon, Dec 6, 2010 at 7:01 PM, Paul Higham <polygame at mac.com> wrote:

> Yes, I believe that the point of the exercise was only to grok the
> semantics and applicability of foldr, it is a rather unnatural way to
> express init.
>

This is a fairly simple solution:

init' :: [a] -> [a]
init' ls = foldr (\x xs n -> if n == 0 then [] else x : xs (n - 1)) (const
[]) ls (length ls - 1)

Of course, *init'* will fail on infinite lists. Tail can be defined using *
foldr* too, though:

tail' :: [a] -> [a]
tail' ls = foldr (x xs (y:ys) -> ys) id ls ls

*takeWhile* and *dropWhile* can be defined similarly, as well as other folds
that "depend" on a value, or require the fold to terminate before the entire
list has been processed.
Anyways, they seem like fairly natural solutions to the problem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20101206/b4823570/attachment.htm>


More information about the Beginners mailing list