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

OCaml: Using fold_right to implement map

I am trying to implement map using List.fold_right in OCaml, in which my function has the same signature and behaviour as List.map. I am not allowed to use pattern matching or any other list functions. Example: SIGNATURE: fold_map: ('a -> 'b) -> 'b…
coder
  • 9
  • 3
-1
votes
1 answer

ASCII art exercise, but I don't understand this solution

I'm doing the codingame exercises and I managed to finish the ASCII Art problem, it was long and difficult but I did it. Now I read one of the solutions and it was done in 5 lines of code! I'm trying to understand it but I'm failing to do so, can…
Esteban
  • 69
  • 1
  • 11
-1
votes
1 answer

Haskell - creating a function definition for the function `all` using foldr

I'm trying to create a function definition for the function all using foldr. p is the predicate. I know this can be done: all p = and . foldr (\x xs -> p x : xs) [] But what I want to do is to shift the function and into the foldr equation. Can…
maxloo
  • 453
  • 2
  • 12
-1
votes
1 answer

How would I get the minPos without a loop and using only foldLeft?

I am stuck figuring out how I may solve this problem. The goal is to find the position of the minimum value of an Iterable (l) without loops. There is a similar problem here: How would I get the minVal without a loop and using only foldLeft? But…
-1
votes
1 answer

Tree From List using FoldTree

I am trying to create a tree from a list. I have written the function using foldl and foldr (latter not shown) treeFromList l | null l = error "no elements in list" | otherwise = foldl insertIfAbsent (initTree (head l)) (tail l) where the…
ZM-
  • 94
  • 1
  • 9
-1
votes
3 answers

Haskell Fold implementation of `elemIndex`

I am looking at Haskell elemIndex function: elemIndex :: Eq a => a -> [a] -> Maybe Int What does Maybe mean in this definition? Sometimes when I call it, the output has a Just or a Nothing What does it mean? How can I interpret this if I were to…
wwww
  • 99
  • 5
-1
votes
2 answers

How does spark interprets type of a column in reduce

I have the following table DEST_COUNTRY_NAME ORIGIN_COUNTRY_NAME count United States Romania 15 United States Croatia 1 United States Ireland 344 Egypt United States 15 …
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
-1
votes
1 answer

Generalized foldr function for trees?

How can I write a generalized foldr function for generic Haskell trees? data (Eq a, Show a) => Tree a = Void | Node a [Tree a] deriving (Eq, Show) treefold :: (Eq a, Show a) => (a -> [b] -> b) -> b -> Tree a -> b I'm stuck at the first…
gremo
  • 47,186
  • 75
  • 257
  • 421
-1
votes
2 answers

Scala : Unable to match the pattern

I am new to Scala and I am trying to execute = following code in Scala: scala> case class FoldExp(firstname : String, lname : String, age : Int, sex : String) defined class FoldExp scala> object Foo{ | def apply(firstname : String, lname : String,…
Anon
  • 320
  • 2
  • 14
-1
votes
1 answer

How to implement foldl using foldr in Erlang?

This is what I thought, any options for a better thought? foldl(Function,Factor,List) -> lists:foldr(fun(X,Acc) -> Acc ++ [X] end,[],lists:foldr(Function,Factor,List)).
Mishal Shah
  • 153
  • 2
  • 2
  • 10
-1
votes
1 answer

Doing an in-order traversal on a binary tree in Haskell using a fold

I'm trying to implement an in-order traversal function using a modified version of fold in Haskell: foldT :: (u -> u -> u) -> (a -> u) -> Tree a -> u foldT f g (Tip a) = g a foldT f g (Node l r) = f (foldT f g l) (foldT f g r) But I'm getting stuck…
newbie
  • 63
  • 7
-1
votes
3 answers

Proper implementation of foldl1

The Haskell Wikibook provides one foldl1 :: (a -> a -> a) -> [a] -> a foldl1 f (x:xs) = foldl f x xs foldl1 _ [] = error "Prelude.foldl1: empty list" that doesnt work. I tried to compile this version of it: myFoldl1 ::…
Tina
  • 17
  • 2
-1
votes
1 answer

Using if inside of fold

I need to count the length of a vector of (bool, i32) where if the bool is true I increment count. I'm using fold to do this: fn main() { let domain = [(true, 1), (false, 2), (true, 3)]; let dom_count = domain.iter() .fold(0, |count,…
HiDefender
  • 2,088
  • 2
  • 14
  • 31
-1
votes
2 answers

Haskell: leafCount function take Tree output Int using fold fun

I define Tree type as: data Tree a = Leaf | Node a (Tree a) (Tree a) deriving (Show, Eq) Here are a fold function : How to write a function of type a-> b -> b -> b for folding a tree, basically same as what I using. Now I want to write a…
o1xhack
  • 87
  • 9
-1
votes
1 answer

How to convert the explicit recursive functions into Higher-Order/Abstract Functions in Scheme

I am wondering how to convert the explicit recursive functions below into a higher-ordered/abstract functions using foldr, map, filter, etc in Scheme/Racket. (define (insertNoDups f element lst) (cond[(empty? lst) (cons element lst)] [(f…