Questions tagged [fold]

In functional programming, a fold, also known variously as reduction, accumulation, or catamorphism, is a type of higher-order function that recursively applies a transformation to a data structure, "collapsing" it to a summary value

In functional programming, a fold, also known variously as , accumulation, or catamorphism, is a type of higher-order function that recursively applies a transformation to a data structure, "collapsing" it to a summary value.

1159 questions
15
votes
2 answers

Since "fold" isn't powerful enough to write a tree pretty-printer with indentation, what high-order combinator is?

Given, for example, the following tree data type: data Tree a = Node [Tree a] | Leaf a deriving Show type Sexp = Tree String How do I express a "pretty" function using an high-order combinator, that prints the tree with proper indentation? For…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
15
votes
3 answers

How to implement delete with foldr in Haskell

I've been studying folds for the past few days. I can implement simple functions with them, like length, concat and filter. What I'm stuck at is trying to implement with foldr functions like delete, take and find. I have implemented these with…
user168064
  • 153
  • 6
15
votes
6 answers

Why can you reverse list with foldl, but not with foldr in Haskell

Why can you reverse a list with the foldl? reverse' :: [a] -> [a] reverse' xs = foldl (\acc x-> x : acc) [] xs But this one gives me a compile error. reverse' :: [a] -> [a] reverse' xs = foldr (\acc x-> x : acc) [] xs Error Couldn't match expected…
Rene
  • 289
  • 1
  • 4
  • 8
14
votes
2 answers

Why doesn't passing Nil to foldLeft work?

When I build a list using foldLeft I often get annoyed at having to explicitly type the injected parameter and wish I could just use `Nil' instead - here's a contrived example: scala> List(1,2,3).foldLeft(List[Int]())((x,y) => y :: x) res17:…
Russell
  • 12,261
  • 4
  • 52
  • 75
14
votes
2 answers

Map, Filter, Foldr in DrRacket/Scheme

Programming language: Scheme/DrRacket We're currently going over map, filter, and foldr in my comp sci class. I understand that all three can be used to create abstract functions, but I am honestly a little confused about the difference between the…
Lukas Pleva
  • 381
  • 2
  • 5
  • 17
14
votes
3 answers

Fold/reduce over List of Futures with associative & commutative operator

Consider the following: import scala.concurrent._ import scala.concurrent.duration.Duration.Inf import scala.concurrent.ExecutionContext.Implicits.global def slowInt(i: Int) = { Thread.sleep(200); i } def slowAdd(x: Int, y: Int) = {…
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
14
votes
3 answers

How do I break out of a pure loop in Haskell without hand-written recursion?

I want to write a function that goes through a list updating an accumulator until that accumulator reaches a certain condition or I get to the end of the list. For example, a product function that stops as soon as its accumulator reaches zero. I…
hugomg
  • 68,213
  • 24
  • 160
  • 246
14
votes
2 answers

Haskell - strict vs non-strict with foldl

I have a question concerning the definition of strict vs non-strict. The Haskell wiki-book for Laziness (http://en.wikibooks.org/wiki/Haskell/Laziness), under the section "Black-box strictness analysis", makes the following assertion: [Assuming a…
shj
  • 1,558
  • 17
  • 23
13
votes
4 answers

How is foldl lazy?

There are lots of good questions and answers about foldl, foldr, and foldl' in Haskell. So now I know that: 1) foldl is lazy 2) don't use foldl because it can blow up the stack 3) use foldl' instead because it is strict (ish) How foldl is…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
13
votes
3 answers

Folding list of options

Given a list [Some 1; Some 2; Some 3] I would like an output Some 6 . Given a list [Some 1; None] should yield None. But I'm finding it a bit more difficult than I had imagined to achieve this in a clean way. The best I could come up with was…
Overly Excessive
  • 2,095
  • 16
  • 31
13
votes
1 answer

terminating a monadic fold early

I've written this so that I can terminate a monadic fold early: myfoldM :: (Monad m) => (a -> b -> m (Maybe a)) -> a -> [b] -> m (Maybe a) myfoldM _ a [] = return $ Just a myfoldM f a (x:xs) = do ma <- f a x; …
ErikR
  • 51,541
  • 9
  • 73
  • 124
12
votes
4 answers

Why does this first Haskell function FAIL to handle infinite lists, while this second snippet SUCCEEDS with infinite lists?

I have two Haskell functions, both of which seem very similar to me. But the first one FAILS against infinite lists, and the second one SUCCEEDS against infinite lists. I have been trying for hours to nail down exactly why that is, but to no avail.…
Charlie Flowers
  • 17,338
  • 10
  • 71
  • 88
12
votes
3 answers

How can a variadic template be used to generate a left-associative expression (aka left fold) in c++11?

I would like to use a c++ template to aggregate (fold) multiple arguments using a binary operation. Such a template could be used as follows: fold(100,10,5) expands to add(add(100, 10), 5) The particular expansion shown above is the "left…
drwatsoncode
  • 4,721
  • 1
  • 31
  • 45
12
votes
1 answer

"Any function on finite lists that is defined by pairing the desired result with the argument list can always be redefined in terms of fold"

I was reading through the paper A tutorial on the universality and expressiveness of fold, and am stuck on the section about generating tuples. After showing of how the normal definition of dropWhile cannot be defined in terms of fold, an example…
Stoof
  • 687
  • 2
  • 6
  • 12
11
votes
1 answer

What is the connection between primitive recursion and catamorphisms?

Using the following catamorphism for natural numbers I can implement various arithmetic algorithms whithout having to deal with recursion: cataNat :: b -> (b -> b) -> Natural -> b cataNat zero succ = go where go n = if (n <= 0) then zero else…
user5536315