Hi.

> while playing with folds and trying to implement `!!` by folding, I came to
> the conclusion that:
>
> - `foldr` is unsuitable because it counts the elements from the end, while
> `!!` needs counting from the start (and it's not tail recursive).

What is the problem with the following definition using foldr?

> index :: Int -> [a] -> a
> index n xs =
>   foldr
>     (\ x r n -> if n == 0 then x else r (n - 1))
>     (const (error $ "No such index"))
>     xs
>     n

Cheers,
  Andres

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to