fold

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

对着背影说爱祢 提交于 2019-12-08 14:24:20
问题 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 define either foldMap or foldr , as all other functions in the class can be derived from either of these two using the default definitions and the instance for lists. So how can foldl be defined in terms of foldr ? Can foldr be defined in terms of foldl , so that we can define a Foldable

How “to fold recursively by a tag” a group of selected (neighbor) tags with XSLT1?

社会主义新天地 提交于 2019-12-08 05:57:50
问题 I was comment the present problem at this other one: the present is more complex because needs a recurrence. Detailing by example: <root> <c>cccc</c> <a gr="g1_1">aaaa</a> <b gr="g1_1">1111</b> <a gr="g2_1" into="g1_1">bbbb</a> <b gr="g2_1" into="g1_1">56565</b> <a gr="g3_1" into="g2_1">BB</a> <b gr="g3_1" into="g2_1">55</b> <a gr="g1_2">xxxx</a> <b gr="g1_2">2222</b> <a gr="g2_2" into="g1_2">wwww</a> <b gr="g2_2" into="g1_2">3433</b> </root> that must be enclosed by fold tags, resulting

How to fold over a discriminated union

六眼飞鱼酱① 提交于 2019-12-07 23:51:28
问题 I'm attempting to implement a fold over a discriminated union. The DU is called Expr, and represents a program expression, and is often recursive. I'm attempting to write a fold that accumulates the result of an operation over the Exprs recursively. Below is my attempt to write the fold. let rec foldProceduralExpr (folder : 's -> Expr list -> 's) (state : 's) (expr : Expr) : 's = let children = match expr with | Series s -> s.SerExprs | Lambda l -> [l.LamBody; l.LamPre; l.LamPost] | Attempt a

Native implementation of reduceRight in JavaScript is wrong

半世苍凉 提交于 2019-12-07 21:48:54
问题 For an associative operation f over the elements of array a , the following relation should hold true: a.reduce(f) should be equivalent to a.reduceRight(f) . Indeed, it does hold true for operations that are both associative and commutative. For example: var a = [1,2,3,4,5,6,7,8,9,0]; alert(a.reduce(add) === a.reduceRight(add)); function add(a, b) { return a + b; } However it doesn't hold true for operations that are associative but not commutative. For example: var a = [[1,2],[3,4],[5,6],[7

Numerical issue with `foldl` and `foldr` in Haskell

只愿长相守 提交于 2019-12-07 16:33:12
问题 I have the following Haskell script which computes the function f(x) = (2- x) - (2^3 - x^3/12) calc x = (x - (x ^ 3) / 12) calc2 x = (calc 2) - (calc x) calcList1 :: [Float] -> Float calcList1 l = foldl (+) 0.0 (map calc2 l) calcList2 :: [Float] -> Float calcList2 l = foldr (+) 0.0 (map calc2 l) test1 :: Float -> Float test1 step = (calcList1 l) - (calcList2 l) where l = [0.0,step..2.0] Function calcList1 and calcList2 run calc2 function on each of list and then uses foldl and foldr

Recursion in treeFold function

倖福魔咒の 提交于 2019-12-07 14:37:42
问题 I have tree data type: data Tree a = Node { rootLabel :: a, -- label value subForest :: [Tree a] -- zero or more child trees } {-- Node (a) [] ..or... Node (a1) [ Node (a2) [..], Node (a3) [..] ] --} and i need to write treeFold function, i think that i need first function that make something with label value and second function that takes two results of func1 and make some final result and so on. I start writing the recursion function treeFold, write function for minimal Tree with 0 child

How “to fold recursively by a tag” a group of selected (neighbor) tags with XSLT1?

南楼画角 提交于 2019-12-07 12:36:24
I was comment the present problem at this other one : the present is more complex because needs a recurrence. Detailing by example: <root> <c>cccc</c> <a gr="g1_1">aaaa</a> <b gr="g1_1">1111</b> <a gr="g2_1" into="g1_1">bbbb</a> <b gr="g2_1" into="g1_1">56565</b> <a gr="g3_1" into="g2_1">BB</a> <b gr="g3_1" into="g2_1">55</b> <a gr="g1_2">xxxx</a> <b gr="g1_2">2222</b> <a gr="g2_2" into="g1_2">wwww</a> <b gr="g2_2" into="g1_2">3433</b> </root> that must be enclosed by fold tags, resulting (after XSLT) in: <root> <c>cccc</c> <fold> <a gr="g1_1">aaaa</a> <b gr="g1_1">1111</b> <fold><a gr="g2_1"

How to fold/accumulate a numpy matrix product (dot)?

会有一股神秘感。 提交于 2019-12-07 10:09:04
问题 With using python library numpy, it is possible to use the function cumprod to evaluate cumulative products, e.g. a = np.array([1,2,3,4,2]) np.cumprod(a) gives array([ 1, 2, 6, 24, 48]) It is indeed possible to apply this function only along one axis. I would like to do the same with matrices (represented as numpy arrays), e.g. if I have S0 = np.array([[1, 0], [0, 1]]) Sx = np.array([[0, 1], [1, 0]]) Sy = np.array([[0, -1j], [1j, 0]]) Sz = np.array([[1, 0], [0, -1]]) and b = np.array([S0, Sx,

reversing a list in OCaml using fold_left/right

时光怂恿深爱的人放手 提交于 2019-12-07 07:32:36
问题 UPDATE - Solution Thanks to jacobm for his help, I came up with a solution. // Folding Recursion let reverse_list_3 theList = List.fold_left (fun element recursive_call -> recursive_call::element) [] theList;; I'm learning about the different ways of recursion in OCaml (for class) and for some exercise, I'm writing a function to reverse a list using different recursion styles. // Forward Recursion let rec reverse_list_forward theList = match theList with [] -> [] | (head::tail) -> (reverse

haskell fold rose tree paths

雨燕双飞 提交于 2019-12-07 05:54:07
问题 let us say we have a tree... data Tree a = Node a [Tree a] deriving (Show) and that tree has some nodes t = Node 1 [Node 2 [Node 3 []], Node 4 [], Node 5 [Node 6 []]] the following function will collect the paths in a tree. paths :: Tree a -> [[a]] paths (Node n []) = [[n]] paths (Node n ns) = map ((:) n . concat . paths) ns like so: *Main> paths t [[1,2,3],[1,4],[1,5,6]] But now how could we fold these paths? Obviously we could do this. Which folds after finding the paths. wastefullFold ::