Questions tagged [foldable]

Foldable is a class of data structures that can be folded to a summary value.

Many of these functions generalize Prelude, Control.Monad and Data.List functions of the same names from lists to any Foldable functor. To avoid ambiguity, either import those modules hiding these names or qualify uses of these function names with an alias for this module.

class Foldable t where

Data structures that can be folded.

Minimal complete definition

[foldMap][5] | [foldr][6]

Full documentation: http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Foldable.html

69 questions
6
votes
3 answers

Why is this implementation a bad instance of the Foldable Typeclass?

I'm working through the wonderful Haskell Book. At the end of the Traversable chapter (21), I need to write an instance for the following Tree: data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) Here is a link to the full code of my…
javinor
  • 674
  • 4
  • 8
6
votes
1 answer

Foldable IntSet

For me, an integer set seems to be a foldable data structure. Why is Data.IntSet not an instance of Foldable? My actual intention is to use find on an IntSet. How can I implement find for Data.IntSet?
lsund
  • 744
  • 5
  • 16
6
votes
1 answer

No instance for Foldable arising from length inside lambda

first question here and completely a noob on haskell, so please be kind with me :) I was playing with the question number 6 of this haskell exercises and in the end came to the solution (or something similar I hope) with this code combinations gr…
iPizarro12
  • 63
  • 5
6
votes
1 answer

Why Monoid is not a requirement for foldr/foldl?

I am looking at the Foldable class in Haskell. Two of the methods fold, foldMap requires a Monoid instance. But foldr or foldl don't have any such constraint. fold :: Monoid m => t m -> m foldMap :: Monoid m => (a -> m) -> t a -> m foldr :: (a -> b…
zeronone
  • 2,912
  • 25
  • 28
5
votes
3 answers

Are there laws for the Foldable typeclass that constrain how Foldable instances can be derived?

I'm experimenting with the Foldable typeclass in Haskell, using the following data type as an example: data Tree a = Empty | Node (Tree a) a (Tree a) If I use the DeriveFoldable GHC extension, it seems to derive a Foldable instance…
DylanSp
  • 1,379
  • 13
  • 27
5
votes
2 answers

What does the type foldMap :: (Monoid m) => (a -> m) -> f a -> m mean and how to implement it?

Can someone explain what the type mean and how to implement this? class Foldable f where foldMap :: (Monoid m) => (a -> m) -> f a -> m Based on https://hackage.haskell.org/package/base-4.9.1.0/docs/Data-Foldable.html#v:foldMap, they explained it…
llamaro25
  • 642
  • 1
  • 7
  • 22
5
votes
4 answers

Is there an equivalent to head/tail for Foldable functors in general?

I would like to express the following Haskell code, using only functor algebra (i.e. - not relying on any specific container type, such as List): ys = zipWith (+) (head xs : repeat 0) (tail xs ++ [y]) It seems to me that there…
dbanas
  • 1,707
  • 14
  • 24
5
votes
2 answers

How do I implement minimum with foldr (or foldMap)?

I want to implement minimum with foldr or foldMap. According to the exercise, it should have this definition: mini :: (Foldable t, Ord a) => t a -> Maybe a -- named "mini" to avoid name clash It sounded pretty straightforward, but I do not know…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
4
votes
1 answer

Why can't a non-polymorphic type implement Foldable in Haskell?

Say I have some simplified lisp-style Expr type as follows data Expr = String String | Cons Expr Expr deriving (Show) I can create lists as Cons-cells of Cons-cells: Cons (String "hello") (Cons (String "World") (String "!")) From this I would…
henrikl
  • 479
  • 2
  • 14
4
votes
3 answers

Can you determine the min or max of a list using only the list monad?

Trying to understand the relation between Monad and Foldable. I am aware that that part of the value of the Monad, Applicative and Functor typeclasses is their ability to lift functions over structure, but what if I wanted to generate a summary…
Conor Quinn
  • 529
  • 4
  • 13
4
votes
1 answer

Foldable instance for a Trie-Set

I have a Set-like data structure, implemented as a Trie, with a definition like this: import qualified Data.Map as M import Data.Foldable (Foldable, foldr) import Prelude hiding (foldr) import Data.Maybe (fromMaybe) data Trie a = Trie { endHere ::…
oisdk
  • 9,763
  • 4
  • 18
  • 36
4
votes
2 answers

Why can't I do `null (Just 5)` in Haskell?

The Hackage documentation for Maybe lists Foldable as one of Maybe's typeclasses. It also lists the following function: null :: Maybe a -> Bool It even links to this function's implementation (from Foldable): null :: t a -> Bool null = foldr (\_ _…
Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
4
votes
3 answers

On Haskell, there a standard function that performs "scan" on a tree?

I have a tree: a :: Tree Double a = Node 1 [Node 20 [Node 300 [Node 400 [], Node 500 []], Node 310 []], Node 30 [], Node 40 []] I want to apply it an scan…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
4
votes
2 answers

Writing a foldMap in Haskell

I am trying to write my own foldMap function as an excersice to learn Haskell Currently it looks like this class Functor f => Foldable f where fold :: Monoid m => f m -> m foldMap :: Monoid m => (a -> m) -> f a -> m …
LordofTurtles
  • 65
  • 1
  • 8
3
votes
3 answers

How to use mapM_ in this situation

I am trying to print different components of result of autocorrelation on different lines: import Data.Vector as V import Statistics.Autocorrelation import Data.Typeable sampleA = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4] main = do let res =…
rnso
  • 23,686
  • 25
  • 112
  • 234