lazy-evaluation

Haskell: comparison of techniques for generating combinations

别来无恙 提交于 2019-11-30 17:40:33
问题 I was doing a few of the 99 Haskell Problems earlier and I thought that exercise 27 ("write a function to enumerate the possible combinations") was interesting as it's a simple concept and it lends itself to multiple implementations. I was curious about relative efficiency so I decided to run a couple of different implementations - results are in the table below. (For reference: Emacs bash ansi-term in LXDE (Ubuntu 14.04) running on VirtualBox; Thinkpad X220; 8gb RAM, i5 64bit 2.4ghz.) TL;DR:

Can someone explain this lazy Fibonacci solution?

匆匆过客 提交于 2019-11-30 17:32:29
问题 This is the code: fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs) When evaluated, fibs is an infinite list of Fibonacci numbers. What I don't understand is how the list is concatenated. zipWith returns a list, so zipping fibs would yield this: 0 : 1 : [1] : [1,2] : [1,2,3] Because 0 : 1 : zipWith (+) [0,1] [1] yields [1] and zipWith (+) [0,1,1] [1,1] yields [1,2] etc). However, when I run the code, I get the correct result. What am I not understanding here? 回答1: Your "Because" is not telling

Defer expression evaluation without using `quote`

妖精的绣舞 提交于 2019-11-30 17:14:24
问题 I have created the following function/example as a generic way to display variable labels in tables and so forth: #' Function to prettify the output of another function using a `var.labels` attribute #' This is particularly useful in combination with read.dta et al. #' @param dat A data.frame with attr `var.labels` giving descriptions of variables #' @param expr An expression to evaluate with pretty var.labels #' @return The result of the expression, with variable names replaced with their

Trying to understand how linq/deferred execution works

爱⌒轻易说出口 提交于 2019-11-30 16:57:06
I have the following methods, part of the logic for performing stratified k-fold crossvalidation. private static IEnumerable<IEnumerable<int>> GenerateFolds( IClassificationProblemData problemData, int numberOfFolds) { IRandom random = new MersenneTwister(); IEnumerable<double> values = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); var valuesIndices = problemData.TrainingIndices.Zip(values, (i, v) => new { Index = i, Value = v }); IEnumerable<IEnumerable<IEnumerable<int>>> foldsByClass = valuesIndices.GroupBy(x => x.Value, x => x.Index) .Select(g

What is the most pythonic way to have a generator expression executed?

不打扰是莪最后的温柔 提交于 2019-11-30 16:16:38
问题 More and more features of Python move to be "lazy executable", like generator expressions and other kind of iterators. Sometimes, however, I see myself wanting to roll a one liner "for" loop, just to perform some action. What would be the most pythonic thing to get the loop actually executed? For example: a = open("numbers.txt", "w") (a.write ("%d " % i) for i in xrange(100)) a.close() Not actuall code, but you see what I mean. If I use a list generator, instead, I have the side effect of

What is the most pythonic way to have a generator expression executed?

梦想的初衷 提交于 2019-11-30 16:07:18
More and more features of Python move to be "lazy executable", like generator expressions and other kind of iterators. Sometimes, however, I see myself wanting to roll a one liner "for" loop, just to perform some action. What would be the most pythonic thing to get the loop actually executed? For example: a = open("numbers.txt", "w") (a.write ("%d " % i) for i in xrange(100)) a.close() Not actuall code, but you see what I mean. If I use a list generator, instead, I have the side effect of creating a N-lenght list filled with "None"'s. Currently what I do is to use the expression as the

how to solve “stack space overflow” in haskell

China☆狼群 提交于 2019-11-30 14:17:49
问题 Running the following program will print "space overflow: current size 8388608 bytes." I have read this and this, but still don't know how to resolve my problem. I am using foldr, shouldn't it be guaranteed to be "tail recursive"? I feel great about Haskell so far until I know I should prevent "space overflow" when using the powerful recursion. :) module Main where import Data.List value a b = let l = length $ takeWhile (isPrime) $ map (\n->n^2 + a * n + b) [0..] in (l, a ,b) euler27 = let

In Haskell, what's the difference between using takeWhile or using a “regular” inequality in this list comprehension?

坚强是说给别人听的谎言 提交于 2019-11-30 11:51:11
I'm trying to learn me a Haskell (for great good), and one of the many different things I'm doing is trying to tackle some Project Euler problems as I'm going along to test my mettle. In doing some of the Fibonacci based problems, I stumbled on and started playing around with the recursive infinite list version of the Fibonacci sequence: fibs = 1 : 2 : zipWith (+) fibs (tail fibs) For one of the PE problems, I needed to extract the subsequence of even Fibonacci numbers less than 4,000,000. I decided to do this with a list comprehension, and in my playing around with the code I stumbled on to

Haskell laziness - how do I force the IO to happen sooner?

六月ゝ 毕业季﹏ 提交于 2019-11-30 11:45:43
I just started learning Haskell. Below is some code written in an imperative style that implements a simple server -- it prints out the HTTP request headers. Besides the fact that I need to rethink it in Haskell, to work with lazy lists and higher order functions, I'd like to see clearly why it does not do what I intended. It is always one behind -- I hit it with a request, nothing happens, hit it again, it prints the first request, hit it the 3rd time, it prints the 2nd request, etc. Why is that? And what is a minimal change to this code that would cause it to print right when the request

Lazily Tying the Knot for 1 Dimensional Dynamic Programming

谁说胖子不能爱 提交于 2019-11-30 11:19:57
Several years ago I took an algorithms course where we were giving the following problem (or one like it): There is a building of n floors with an elevator that can only go up 2 floors at a time and down 3 floors at a time. Using dynamic programming write a function that will compute the number of steps it takes the elevator to get from floor i to floor j . This is obviously easy using a stateful approach, you create an array n elements long and fill it up with the values. You could even use a technically non-stateful approach that involves accumulating a result as recursively passing it