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

haskell tree using fold operation

How would I go about writing a contains, balanced function given this definition using folds. data Tree a = Tree a [Tree a] treeFold :: (a -> [b] -> b) -> Tree a -> b treeContains :: Eq a => a -> Tree a -> Bool treeBalanced :: Tree a -> Bool Note…
Ishan
  • 3,931
  • 11
  • 37
  • 59
0
votes
4 answers

Scala: using foldl to add pairs from list to a map?

I am trying to add pairs from list to a map using foldl. I get the following error: "missing arguments for method /: in trait TraversableOnce; follow this method with `_' if you want to treat it as a partially applied function" code: val…
Anton Ashanin
  • 1,817
  • 5
  • 30
  • 43
0
votes
1 answer

Scala assumes wrong type when using foldLeft

I am trying to create a cross product function in Scala, where k is the number of times I build the cross product. val l = List(List(1), List(2), List(3)) (1 to k).foldLeft[List[List[Int]]](l) { (acc: List[List[Int]], _) => for (x <- acc; y <-…
knub
  • 3,892
  • 8
  • 38
  • 63
0
votes
2 answers

foldl operation in SML

I perform the following foldl operation foldl (fn (acc,y) => if acc>y then acc else y+1) 0 [1,3] So, I expect this to produce me an result of 4 but it produces an output of 3. What am I missing ? My trace is something like this: acc: 0 y: 1 acc:…
Sibi
  • 47,472
  • 16
  • 95
  • 163
0
votes
4 answers

Duplicates removal with foldl

I'm trying to write my implementation of remdps, function, which removes nearest duplicates in a list. For example: "aaabbbsscaa" should became "absca". I have to use foldl. Here is my attempt: helper :: Eq a => [a] -> a -> [a] helper [] ele =…
ciembor
  • 7,189
  • 13
  • 59
  • 100
0
votes
1 answer

Reversing a list using foldr

How can you define a function to reverse a list as only a single call to foldr? It should look like this (define (rev l) (foldr ___________________________ This is a practice exam question, for which the teacher decided not to give answers for…
Outback
  • 542
  • 2
  • 8
  • 20
0
votes
6 answers

haskell foldr manipulation with lists

Given a list of sequence of negative and positive numbers, how can I partition them into sequences of negative and positive numbers using foldr? For example [1,2,3,-1,-2,-3,1,2,3] i will get [[1,2,3],[-1,-2,-3],[1,2,3]] A few doubts How do I know…
kkh
  • 4,799
  • 13
  • 45
  • 73
0
votes
1 answer

MapThread in mathematica with one variable constant

I would Like to fold (in order to sum) a MapThread over two lists using a function of three variabled, where the third variable is constant over the Mapthread. I would like to accomplish something like this: nList = {}; For[i = 0, i <= Length[N0],…
slev
  • 43
  • 5
0
votes
1 answer

jquery and fold effect: hide directly without animation

I'm trying to animate a div, which has 3-4 child divs, with the fold effect: $("#main div").each(function () { $(this).click(function () { $(this).effect('fold',{},'2000',callback); }) }); When I click on the main div it will…
kangun
  • 126
  • 2
  • 11
0
votes
0 answers

do a fold without Photoshop

I'm trying to make a fold using only CSS, so far this is what I have: JsFiddle I'm not sure whether it looks like a fold, I think it can be done better. How can I make this look better? Is there a tutorial somewhere that addresses this issue? ps…
Youss
  • 4,196
  • 12
  • 55
  • 109
0
votes
2 answers

scala: fold over tuple success

With an expression that returns an Either[Fail, TupleX], how do I fold over the result without having to define local vals in the success block? // returns Either[Fail, Tuple2[String, String]] val result = for{ model <- bindForm(form).right key …
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
0
votes
1 answer

Custom fold in Cross Validation (Weka)

Is it possible to create custom fold for Cross Validation in Weka. What I want is to have that each fold contains only the records that have a certain value for an attribute (e.g ZIP code).Is this possible?
Titus Pullo
  • 3,751
  • 15
  • 45
  • 65
-1
votes
1 answer

Is there an unfolding-folding operation, that will perform a matrix repetition in pytorch?

I have the following tensor in pytorch: i1 = torch.randn(1, 32, 320, 640) I would like to extract the following sliding blocks [:,:,i,0:15], [:,:,i+80,0:15], [:,:,i+160,0:15], [:,:,i+240,0:15] [:,:,i,1:16], [:,:,i+80,1:16], [:,:,i+160,1:16],…
Ilias
  • 1
  • 1
-1
votes
1 answer

What does map' f xs = foldr (\y ys -> (f y):ys) [] xs. mean?

I'm new to Haskell and I want to understand what this syntax means. This is the context of the function: map' :: (a -> b) -> [a] -> [b] map' f xs = foldr (\y ys -> (f y):ys) [] xs It's defining the map function from the prelude in terms of foldr.…
-1
votes
1 answer

Dart fold with null safety

I have the following code that sums cash for people with the same name using list fold. void main() { List> people = [{'name': 'Jim', 'cash':44.86},{'name': 'Jim', 'cash':40.55},{'name': 'Bob', 'cash':10.99},{'name': 'Bob',…
ssmc
  • 21
  • 6