Skip to content

sumFold example could be clearer with foldr #7

@tov

Description

@tov

I wonder if the sumFold example would be clearer if it were written using foldr instead of with foldl. In particular, because sumManual is written with the same kind of recursion as foldr, this would be easier to see:

Turns out, the definition of foldl contains all of that sweet recursive machinery needed to power our manual sum function:

foldr f z []     = z
foldr f z (x:xs) = x `f` foldr f z xs

sumManual []     = 0
sumManual (x:xs) = x  +  sumManual xs

See how well they line up?

(I really enjoy how this is written overall.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions