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
-1
votes
1 answer

Returning the leaves of a binary Tree with TreeFold

data BinTree el = EmptyBinTree | NonEmptyTree (BinTree el) el (BinTree el) deriving (Show) binTreeFold :: (acc -> el -> acc -> acc) -> acc -> BinTree el -> acc binTreeFold _ acc EmptyBinTree = acc binTreeFold f…
user3742929
  • 360
  • 3
  • 17
-1
votes
2 answers

About an error with foldl1 (Haskell)

I have a data type that goes: BinHeap a = [BinTree a] BinTree a = Node a i (BinHeap a) I want a function to go through a Bintree a and give me the smallest a. extractMin :: BinHeap a -> a extractMin ps = foldl1 (\(Node x y z) (Node x' y' z') ->…
Daniel Hernandez
  • 635
  • 1
  • 11
  • 22
-1
votes
1 answer

Is it OK to use Fold without reducing number of dimensions?

For example, one wants to use Fold function to implement an unique (or DeleteDuplicates) function, which accepts 1-dimensional array and returns the same, but shorter. I am against it, but I can't find proof in any Wikipedia, why it is a bad…
Nakilon
  • 34,866
  • 14
  • 107
  • 142
-1
votes
1 answer

SML currying ( functional prog)?

I asked this question a few days ago but now I have more understanding on the subject. But I still get problem that operator and operand dont agree: Using ListPair.foldr I need to create a function zipWith that combines pairwise two lists. The type…
user2012107
  • 9
  • 1
  • 3
-2
votes
1 answer

Haskell, Applying function to each item in a list

hey there another haskell question i have a function of type f :: CSP -> Assignment -> Var -> Int -> CSP i want to write a function with the type g :: CSP -> Assignment -> Var -> [Int] -> CSP which applies the original function to all of the…
user1226239
  • 69
  • 1
  • 1
  • 6
-2
votes
1 answer

What kinds of recursions are used in the definitions of `foldr` and `foldl`?

foldr :: (a -> b -> b) -> b -> [a] -> b foldr f v [] = v foldr f v (x:xs) = f x (foldr f v xs) and foldl :: (a -> b -> a) -> a -> [b] -> a foldl f v [] = v foldl f v (x:xs) = …
Tim
  • 1
  • 141
  • 372
  • 590
-2
votes
1 answer

Write haskell function using foldr

I have the following function and should write it with foldr. f [] = [] f (x:xs) | x == 0 = case f xs of [] -> [] ys@(y:_) -> if y == 0 then ys else x : ys | otherwise = x : f xs This function basically removes all…
K Split X
  • 3,405
  • 7
  • 24
  • 49
-2
votes
1 answer

Error on fold when reducing in Spark / Scala

Given that I have a dataframe with some columns: Why does this not work? val output3b = input.withColumn("sum", columnsToConcat.foldLeft(0)((x,y)=>(x+y))) notebook:16: error: overloaded method value + with alternatives: (x: Int)Int (x:…
thebluephantom
  • 16,458
  • 8
  • 40
  • 83
-2
votes
1 answer

Explanation for group by 3 elements from a list function using FOLD in OCAML

I have a piece of code that does the following: group 3 elements of a list of n elements. The main function is called group_by_3. For example, executing group_by_3 [1;2;3;4;5;6;7] will give me ([1;2;3],[4;5;6],[7]). let group_by_3 lst = let accum…
John Dunn
  • 11
  • 6
-2
votes
2 answers

Haskel '!!' defined in foldl

I just started learning Haskell and I'm trying to define the "Get n'th element of list" (with the !! operator) in a function which uses foldl. I now defined it without foldl, just making use of recursion. I wondered if anybody could tell me how to…
Steven
  • 1,123
  • 5
  • 14
  • 31
-3
votes
4 answers

Can `foldr` and `foldl` be defined in terms of each other?

Can foldr and foldl be defined in terms of each other? Programming in Haskell by Hutton says What do we need to define manually? The minimal complete definition for an instance of the Foldable class is to …
Tim
  • 1
  • 141
  • 372
  • 590
-3
votes
2 answers

Can `foldMap f` be defined equivalently in terms of `fold/foldl` and `f`?

In Foldable t, foldMap is defined based on foldr foldMap :: Monoid m => (a -> m) -> t a -> m foldMap f = foldr (mappend . f) mempty Can foldMap f be defined equivalently in terms of fold and f? I guess foldMap is some kind of composition of fold…
Tim
  • 1
  • 141
  • 372
  • 590
-3
votes
1 answer

How can I drop nth element of a list using foldl?

dropnth' :: [a] -> Int -> [a] dropnth' xs n = foldl (\a b -> if (last a) == xs!!n then a else b ++ []) [head xs] xs I was trying to solve this "dropping every nth element of a list" question using foldl, but I'm getting an error. How can I do…
can
  • 36
  • 2
-3
votes
1 answer

Rust: Folding a set of tuples

I'm working on a pet project and I've found myself with a HashSet of tuples. Specifically HashSet<(String,generic_array::GenericArray, u64)> I'd like to sum the u64 element and can do so with a for loop without…
Darakian
  • 619
  • 2
  • 9
  • 22
-3
votes
2 answers

how to write a foldr HOF in Haskell

i want to add some amounts which in my tuple. i want to take that out and total it and show. i want to use Foldr function. can someone help me out how to do
Nubkadiya
  • 3,285
  • 13
  • 40
  • 45
1 2 3
77
78