Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Functional programming recursion schemes (lazzaro.com.ar)
31 points by llazzaro on Nov 3, 2014 | hide | past | favorite | 10 comments


I couldn't understand the authors implementation. I found this in prelude. So much simpler.

dropWhile :: (a -> Bool) -> [a] -> [a]

dropWhile p [] = []

dropWhile p xs@(x:xs')

            | p x       =  dropWhile p xs'

            | otherwise =  xs
I feel that dropWhile isn't that kind of operation fold was designed for.

EDIT: I was somewhat wrong. After some googling I found this clever implementation :

dropWhilePair :: (a -> Bool) -> [a] -> ([a], [a])

dropWhilePair p = foldr f v

  where

    f x (ys,xs) = (if p x then ys else x : xs, x : xs)

    v            = ([],[])
But this returns a tuple whose first element is the answer we need.


A strangely glaring error

    prod (x:xs) = x * (sum xs)


That's why I wished anaphoric recursion was more used.

    prod (x:xs) = x * (self xs)


    prod' self [] = 1
    prod' self (x:xs) = x * self xs

    prod = fix prod'


Maybe my Haskell is a bit rusty, but I don't get the definition of recr. What is 'f' in the following?:

    recr base combine (x:xs) = f x xs (recr base combine xs)


it is a typo. should be 'combine' (because, the way it is now, the presence of the 'combine' argument serves no purpose).


It's not clear what programming language is being used in this blog entry from April 2014, but it does show a trick or two using a higher level function as input to the foldr recursive function.

This feels like it has some examples of how to use it and how NOT to use it, but there is some confusion expressed by the writer as well.


It's not stated anywhere, but it's Haskell. Usually a safe bet when presented with a functional programming language that is not lisp-like normuses a c-like syntax.


If you really want to go down the rabbit hole on stuff like this, check out bouncy folds

https://github.com/quchen/articles/blob/master/useful_techni...


Also valuable in this space is writing `foldl` in terms of `foldr` and writing operations on Hughe's lists.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: