fold

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

你说的曾经没有我的故事 提交于 2019-12-13 08:24:11
问题 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 = ( [], [], 0 ) in let f (all_groups, current_group, size) x = if size = 3 then ( (List.rev current_group) :: all_groups, [x], 1 ) else ( all_groups, x::current_group, size+1) in let (groups, last, _) = List.fold_left f accum lst in List.rev ( List

Scala assumes wrong type when using foldLeft

ⅰ亾dé卋堺 提交于 2019-12-13 03:07:54
问题 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 <- l) yield x ::: l } However, this code does not compile: test.scala:9: error: type mismatch; found : List[List[Any]] required: List[List[Int]] for (x <- acc; y <- l) ^ Why does it ever think I have a List[Any] 's there? Clearly everything I am dealing

Why does foldBack not execute the same side-effects that fold does?

家住魔仙堡 提交于 2019-12-12 19:35:37
问题 I was working through the answers to Example of the difference between List.fold and List.foldBack trying to get my head around the difference between fold and foldBack . I understand the difference in application order now, but there's a difference in side-effects that I don't understand. I used List.fold and List.foldBack for my testing. My accumulator functions that were basically equivalent to :: , so that accumulation order would matter. The accumulator functions I used were as follows:

How to define the fibonacci sequence using a fold for natural numbers?

大兔子大兔子 提交于 2019-12-12 19:14:28
问题 I am currently learning folds in the sense of structural recursion/catamorphisms. I implemented power and factorial using a fold for natural numbers. Please note that I barely know Haskell, so the code is probably awkward: foldNat zero succ = go where go n = if (n <= 0) then zero else succ (go (n - 1)) pow n = foldNat 1 (n*) fact n = foldNat 1 (n*) n Next I wanted to adapt the fibonacci sequence: fib n = go n (0,1) where go !n (!a, !b) | n==0 = a | otherwise = go (n-1) (b, a+b) With fib I

Is there some functions like zip and fold in Perl?

≯℡__Kan透↙ 提交于 2019-12-12 12:11:51
问题 I want to use some function such as "zip", "fold" and "map" in perl. (Just like in Haskell.) I found map and it works well. Then, is there zip and fold? Thank you very much. 回答1: I've implemented many of those functions (and even Haskell-like lazy ones) in my module List::Gen use List::Gen qw(zip reduce); my @list = zip [1 .. 4], ['a' .. 'd']; my $str = reduce {$a . $b} @list; say $str; # 1a2b3c4d Or using the glob function to build the ranges: use List::Gen 'glob'; say <1 .. 4>->zip(<a .. d>

Generalizing fold such that it becomes expressive enough to define any finite recursion?

孤街醉人 提交于 2019-12-12 09:37:31
问题 So, there is something known as a "universal property of fold", stating exactly following: g [] = i; g (x:xs) = f x (g xs) <=> g = fold f i However, as you probably now, there are rare cases like dropWhile , which can not be redefined as fold f i unless you generalize it . The simplest yet obvious way to generalize is to redefine universal property: g' y [] = j y; g' y (x:xs) = h y x xs (g' y xs) <=> g' y = fold (?) l At this point I can make my assumption: I assume existence of somewhat

Easy way to break foldl

会有一股神秘感。 提交于 2019-12-12 08:51:52
问题 I need to break from foldl. Here is a dummy example how to break from fold when I count sum of values in a list and meet too big value (i.e. 10) L = [1,2,3,4,10,5,6,7], Res = try lists:foldl( fun(I, Value) -> if (I < 10) -> Value + I; true -> throw({too_big_value, Value}) end end, 0, L) catch throw:{too_big_value, Value} -> Value end, Res. I know this example is artificial but are there any nice method to break out fold (I know that fold always scan the whole structure)? Please note, that i

How should I conceptualize a right fold vs a left fold?

不羁的心 提交于 2019-12-12 04:56:36
问题 Should I conceptualize a right fold as "folding the list to the right" or as "folding a list from the right"? In other words, does a right fold go from left-to-right or from right-to-left ? 回答1: Right fold goes from right to left. Left fold goes from the left, like you read a text - from left to right. 来源: https://stackoverflow.com/questions/28461908/how-should-i-conceptualize-a-right-fold-vs-a-left-fold

Very basic task using foldr

我是研究僧i 提交于 2019-12-12 01:23:41
问题 I just used a very simple example used in some lecture notes with my ghci: foldr (:) [] 1 2 expecting the result [1,2] However, I get an error. I get an error everytime when I try to use ++ or : as the function given to foldr. Apparently I am making some pretty obvious mistake but I still cannot seem to find it. Can anyone help? 回答1: You used foldr like a variadic function by passing it two arguments 1 and 2 instead of [1, 2] . When you run into trouble like that, just check the function's

haskell foldr manipulation with lists

亡梦爱人 提交于 2019-12-11 19:10:19
问题 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 that the previous partition that I have already compared if of the same sign as the one I am comparing current? How do I add the element to the list? I tried something like [x]:y but what I get was each element as a list and concatenated together,