lazy-evaluation

What causes “Error C stack overflow” in haskell usually

点点圈 提交于 2019-12-01 20:38:56
问题 What are the usual causes of "Error C stack overflow" in the Hugs Haskell implementation. 回答1: This can come up if you are used to functional languages in which it is common to do tail-recursion factorization. Say you have a function: sum = go 0 where go accum [] = accum go accum (x:xs) = go (accum+x) xs Which, incidentally, is the same as sum = foldl (+) 0 If we evaluate the function we can see the problem: sum [1,2,3,4] go 0 [1,2,3,4] go (0+1) [2,3,4] go ((0+1)+2) [3,4] go (((0+1)+2)+3) [4]

Tying the knot in Clojure: circular references without (explicit, ugly) mutation?

戏子无情 提交于 2019-12-01 20:18:27
In my answer at Clojure For Comprehension example I have a function that processes its own output: (defn stream [seed] (defn helper [slow] (concat (map #(str (first slow) %) seed) (lazy-seq (helper (rest slow))))) (declare delayed) (let [slow (cons "" (lazy-seq delayed))] (def delayed (helper slow)) delayed)) (take 25 (stream ["a" "b" "c"])) ("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba") It works by creating a forward reference ( delayed ) which is used as the second entry in a lazy sequence ( slow ).

Changing names of resulting variables in custom dplyr function

被刻印的时光 ゝ 提交于 2019-12-01 20:18:04
Background In order to speed up generating grouped summaries across multiple tables; as I'm doing most of that while in dplyr workflow, I've drafted a simple function that generates the desired metrics # Function to generate summary table generate_summary_tbl <- function(dataset, group_column, summary_column) { group_column <- enquo(group_column) summary_column <- enquo(summary_column) dataset %>% group_by(!!group_column) %>% summarise( mean = mean(!!summary_column), sum = sum(!!summary_column) # Other metrics that need to be generated frequently ) %>% ungroup -> smryDta return(smryDta) }

What causes “Error C stack overflow” in haskell usually

喜你入骨 提交于 2019-12-01 20:10:28
What are the usual causes of "Error C stack overflow" in the Hugs Haskell implementation. This can come up if you are used to functional languages in which it is common to do tail-recursion factorization. Say you have a function: sum = go 0 where go accum [] = accum go accum (x:xs) = go (accum+x) xs Which, incidentally, is the same as sum = foldl (+) 0 If we evaluate the function we can see the problem: sum [1,2,3,4] go 0 [1,2,3,4] go (0+1) [2,3,4] go ((0+1)+2) [3,4] go (((0+1)+2)+3) [4] go ((((0+1)+2)+3)+4) [] (((0+1)+2)+3)+4 Now to evaluate that expression Haskell uses a stack: EXPR | STACK

Lazy quantifier {,}? not working as I would expect

非 Y 不嫁゛ 提交于 2019-12-01 18:40:32
I have an issue with lazy quantifiers. Or most likely I misunderstand how I am supposed to use them. Testing on Regex101 My test string is let's say: 123456789D123456789 .{1,5} matches 12345 .{1,5}? matches 1 I am OK with both matches. .{1,5}?D matches 56789D !! I would expect it to match 9D Thanks for clarifying this. First and foremost, please do not think of greediness and laziness in regex as means of getting the longest/shortest match . "Greedy" and "lazy" terms only pertain to the rightmost character a pattern can match, it does not have any impact on the leftmost one. When you use a

Why this Haskell code never terminates?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 18:13:15
I recently wrote some Haskell code and it never terminates. After I carefully examined my code, the problem boiled down to the following code piece main :: IO () main = print $ let a = 10 in let a = a in a :: Int I guess this must have something to do with the laziness of Haskell since the same code terminates in OCaml. However, if I wrote the following code instead main :: IO () main = print $ let a = 10 in let b = a in b :: Int the code would have no problem terminating at all. I can't get the reason since in the original code, the two a's should be considered as two different variables. I

Does Excel evaluate both result arguments supplied to the IF function?

▼魔方 西西 提交于 2019-12-01 16:58:02
Excel's if function takes three arguments, a condition, an if-true value, and an if-false value. Does Excel work out the value of all three arguments, or does it only work out the value of the condition and the corresponding result? Clarification: I'm not wondering what the result of the if will be, I'm wondering whether or not it calculates the value of all arguments before calculating the result of the function. This is equivalent to asking whether or not the if function uses lazy or strict evaluation. For example, the following pseudocode: x = 5; print x>2 ? "Bigger" : "Smaller" + 1/0 would

Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?

╄→гoц情女王★ 提交于 2019-12-01 16:28:06
As I understood call-by-name parameters of a method, the corresponding argument expression will not be evaluated when passing it to the method, but only when (and if) the value of the parameter is used in the method body. In the following example, however, this is only true in the first two method calls, but not in the third one, although it should be a merely syntactical variation of the second case!? Why is the argument expression evaluated in the third method call? (I tested this code using Scala 2.11.7) class Node(x: => Int) class Foo { def :: (x: =>Int) = new Node(x) // a right

Sql Server predicates lazy?

匆匆过客 提交于 2019-12-01 15:01:12
问题 I have a query: SELECT someFields FROM someTable WHERE cheapLookup=1 AND (CAST(someField as FLOAT)/otherField)<0.9 So, will the CAST and division be performed in the case that cheapLookup is 0 ? If not, how can I avoid the calculation in this case? 回答1: It depends on the query plan, which is determined by the estimated cost of each considered alternative plan that would produce correct results. If the predicate 'cheapLookup = 1' can use an index, and it is sufficiently selective, SQL Server

F# lazy pixels reading

最后都变了- 提交于 2019-12-01 13:46:42
I want to make a lazy loading of image pixels to the 3 dimensional array of integers. For example in simple way it looks like this: for i=0 to Width for j=0 to Height let point=image.GetPixel(i,j) pixels.[0,i,j] <- point.R pixels.[1,i,j] <- point.G pixels.[2,i,j] <- point.B How it can be made in lazy way? What would be slow is the call to GetPixel . If you want to call it only as needed, you could use something like this: open System.Drawing let lazyPixels (image:Bitmap) = let Width = image.Width let Height = image.Height let pixels : Lazy<byte>[,,] = Array3D.zeroCreate 3 Width Height for i =