fold

Stack space overflow with the ST monad

假装没事ソ 提交于 2019-12-10 18:05:00
问题 The following short Haskell program is intended to count a list of items from a file. The version using foldl' works fine, but the version using the ST Monad gives a stack space overflow message. Clearly there's a space leak of some sort here, but I haven't been able to solve it. The really interesting part is that the ST monad is supposed to be doing in-place updates and shouldn't be letting resources grow like this, although that may only pertain to main memory and not stack space. Could

Variations of folds on Haskell Trees

偶尔善良 提交于 2019-12-10 17:33:32
问题 Given a tree defined to be: data Tree a = Leaf | Node (Tree a) a (Tree a) deriving (Eq, Show) I want to use the function: foldTree :: (b -> a -> b -> b) -> b -> Tree a -> b foldTree _ b Leaf = b foldTree f b (Node lt x rt) = f (foldTree f b lt) x (foldTree f b rt) To be able to create equivalents of the normal foldr and foldl as: foldTreeR :: (a -> b -> b) -> b -> Tree a -> b foldTreeL :: (b -> a -> b) -> b -> Tree a -> b I thought these would be fairly straightforward since their definitions

Python, recursively reduce a list (combinations/permutations)

那年仲夏 提交于 2019-12-10 17:25:15
问题 I'm trying to make a generic function that would reduce a list like so : func(['a','b','c'],str.join) # --> ['a','b','c','ab','ac','bc','abc'] func(['a','b','c'],lambda: a,b:a+'x'+b) # --> ['a','b','c','axb','axc','bxc','axbxc'] I don't really know how to do it. I did a few tries, but none was successful. I'm pretty sure there is a way to do it with reduce but i'm not very comfortable with the use of this function. Here are some attempts : reduce(lambda a,b:[a,b,str(a)+str(b)],['a','b','c'])

Why does this Clojure Reducers r/fold provide no perf benefit?

旧巷老猫 提交于 2019-12-10 13:30:47
问题 I'm wondering why the code below provides no speedup in the case of r/fold? Am I misunderstanding something about reducers? I'm running it on a pretty slow (although with 2 cores) Ubuntu 12.04 dev box, both through emacs and lein run, each with the same results. (require '[clojure.core.reducers :as r]) (.. Runtime getRuntime availableProcessors) ;; 2 (let [n 80000000 vs #(range n)] (time (reduce + (vs))) (time (r/fold + (vs))) "Elapsed time: 26076.434324 msecs" "Elapsed time: 25500.234034

How do I break out of a pure loop in Haskell without hand-written recursion?

落花浮王杯 提交于 2019-12-10 02:08:31
问题 I want to write a function that goes through a list updating an accumulator until that accumulator reaches a certain condition or I get to the end of the list. For example, a product function that stops as soon as its accumulator reaches zero. I know how to code it by writing the recursion by hand: {-# LANGUAGE BangPatterns #-} prod :: [Integer] -> Integer prod xs = go 1 xs where go 0 _ = 0 go !acc [] = acc go !acc (x:xs) = go (acc * x) xs but is there a way to code this using folds and other

Fold / Recursion over Multiway Tree in f#

瘦欲@ 提交于 2019-12-09 16:24:14
问题 I am trying to adapt Brian's Fold for Bianary Trees (http://lorgonblog.wordpress.com/2008/04/06/catamorphisms-part-two/) to apply for Multiway trees. Summarizing from Brian's Blog: Data structure: type Tree<'a> = | Node of (*data*)'a * (*left*)Tree<'a> * (*right*)Tree<'a> | Leaf let tree7 = Node(4, Node(2, Node(1, Leaf, Leaf), Node(3, Leaf, Leaf)), Node(6, Node(5, Leaf, Leaf), Node(7, Leaf, Leaf))) Binary Tree Fold function let FoldTree nodeF leafV tree = let rec Loop t cont = match t with |

Is foldl ever preferable to its strict cousin, foldl'?

蓝咒 提交于 2019-12-09 14:26:46
问题 Haskell has two left fold functions for lists: foldl , and a "strict" version, foldl' . The problem with the non-strict foldl is that it builds a tower of thunks: foldl (+) 0 [1..5] --> ((((0 + 1) + 2) + 3) + 4) + 5 --> 15 This wastes memory, and may cause a stack overflow if the list has too many items. foldl' , on the other hand, forces the accumulator on every item. However, as far as I can tell, foldl' is semantically equivalent to foldl . Evaluating foldl (+) 0 [1..5] to head normal form

F# - Breaking a List into Concatenated Strings by an Interval

青春壹個敷衍的年華 提交于 2019-12-09 04:33:26
I have a list of email addresses, and I need to send an email notification to each address. I'd like to do this in blocks of 25 addresses at a time. Is there any quick way in a functional language like F# to "fold" (concatenate) 25 emails addresses together... each separated by a semicolon. I know there is the String.Split method in .NET, but I need to concat 25 at a time. What is the most elegant way to perform this in F#? Here's a way to break into groups of at most N: // break a sequence up into a sequence of arrays, // each of length at most 'n' let Break n (s:seq<_>) = seq { use e = s

How many arguments takes the foldr function of Haskell?

不想你离开。 提交于 2019-12-08 21:41:27
问题 I am new to Haskell and I am reading the book "Real World Haskell". In the Chapter 4 of the book the author asks as an exercise to rewrite the groupBy function using fold. One of the readers of the book (Octavian Voicu ) gave the following solution: theCoolGroupBy :: (a -> a -> Bool) -> [a] -> [[a]] theCoolGroupBy eq xs = tail $ foldr step (\_ -> [[]]) xs $ (\_ -> False) where step x acc = \p -> if p x then rest p else []:rest (eq x) where rest q = let y:ys = acc q in (x:y):ys My question is

foldl / foldr query

删除回忆录丶 提交于 2019-12-08 15:53:47
问题 I'm a beginner at Haskell, and even after reading several explanations of foldr/foldl, I can't understand why I'm getting different results below. What is the explanation? Prelude> foldl (\_ -> (+1)) 0 [1,2,3] 4 Prelude> foldr (\_ -> (+1)) 0 [1,2,3] 3 Thanks! 回答1: In the foldl case, the lambda is being passed the accumulator as the first argument, and the list element as the second. In the foldr case, the lambda is being passed the list element as the first argument, and the accumulator as