lazy-evaluation

how to solve “stack space overflow” in haskell

风格不统一 提交于 2019-11-30 10:07:23
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 tuple_list = [value a b | a <-[-999..999] , b <- [-999..999]] in foldr (\(n,a,b) (max,v) -> if n > max

Logical short-circuit inside a function handle

十年热恋 提交于 2019-11-30 09:14:59
问题 I have a function handle that operates on 2d arrays of arbitrary size: R2T = @(DL1,DL2) arrayfun(@(DL1,DL2)... 1/(fzero(@(x)fFitObj1(x)./fFitObj2(x)-... DL1./DL2,[minLim maxLim])) ... ,DL1,DL2) - C1; Here's a bottom-up breakdown of what it does: fzero(@(x)fFitObj1(x)./fFitObj2(x)-DL1./DL2,[minLim maxLim]) - This bit looks for a zero of the considered function on the interval [minLim maxLim] , where fFitObj1 and fFitObj2 are function handles available from before, C1 is some known constant and

MATLAB Lazy Evaluation in Dependent Property

怎甘沉沦 提交于 2019-11-30 09:04:30
I have a class with a few properties that are dependent, but that I'd really like to calculate only once. I've just about concluded that using lazy evaluation on a dependent class property in MATLAB is either impossible or a bad idea. The original plan was to have a private logical flag for each (public) property that needs updating and to have the constructor set it to true. Then when the property accessor was called, it would check that flag and calculate the value and store it (in another private property) only if required. If the flag were false, it would simply return a copy of the cached

What is “Call By Name”?

陌路散爱 提交于 2019-11-30 08:29:13
I'm working on a homework assignment where we are asked to implement an evaluation strategy called "call by name" in a certain language that we developed (using Scheme). We were given an example in Scala , but I don't understand how "call by name" works and how it is different to "call by need"? Call-by-need is a memoized version of call-by-name (see wikipedia ). In call-by-name, the argument is evaluated every time it is used , whereas in call-by-need, it is evaluated the first time it is used, and the value recorded so that subsequently it need not be re-evaluated. Call by name is a a

Binary Serialization for Lists of Undefined Length in Haskell

五迷三道 提交于 2019-11-30 08:25:21
I've been using Data.Binary to serialize data to files. In my application I incrementally add items to these files. The two most popular serialization packages, binary and cereal, both serialize lists as a count followed by the list items. Because of this, I can't append to my serialized files. I currently read in the whole file, deserialize the list, append to the list, re-serialize the list, and write it back out to the file. However, my data set is getting large and I'm starting to run out of memory. I could probably go around unboxing my data structures to gain some space, but that

Sequence vs LazyList

我是研究僧i 提交于 2019-11-30 06:42:37
I can't wrap my head around the differences between sequence and LazyList . They're both lazy and potentially infinite. While seq<'T> is IEnumerable<'T> from .NET framework, LazyList is included in F# PowerPack . In practice, I encounter sequences much more often than LazyList s. What are their differences in terms of performance, usage, readability, etc? What are reasons for such a bad reputation of LazyList compared to that of seq ? Daniel LazyList computes each element only once regardless of how many times the list is traversed. In this way, it's closer to a sequence returned from Seq

Can a thunk be duplicated to improve memory performance?

一个人想着一个人 提交于 2019-11-30 06:24:41
One of my struggles with lazy evaluation in Haskell is the difficulty of reasoning about memory usage. I think the ability to duplicate a thunk would make this much easier for me. Here's an example. Let's create a really big list: let xs = [1..10000000] Now, let's create a bad function: bad = do print $ foldl1' (+) xs print $ length xs With no optimizations, this eats up a few dozen MB of ram. The garbage collector can't deallocate xs during the fold because it will be needed for calculating the length later. Is it possible to reimplement this function something like this: good = do (xs1,xs2)

Pass parameters to constructor, when initializing a lazy instance

北城余情 提交于 2019-11-30 06:06:41
public class myClass { public myClass(String InstanceName) { Name = InstanceName; } public String Name { get; set; } } // Now using myClass lazily I have: Lazy<myClass> myLazy; Console.WriteLine(myLazy.Value.Name); My question is how to pass InstanceName to myClass constructor when we are using a lazy instance ? Try this: Lazy<myClass> myLazy = new Lazy<myClass>(() => new myClass(InstanceName)); Remember that the expression is evaluated lazily, so if you change the value of the variable InstanceName before the constructor is called it might not do what you expect. Lazy has two ways to

What's the best way to return an Enumerator::Lazy when your class doesn't define #each?

冷暖自知 提交于 2019-11-30 05:44:54
问题 Enumerable#lazy relies on your enumerable providing an #each method. If your enumerable doesn't have an #each method you can't use #lazy . Now Kernel#enum_for and #to_enum provide the flexibility to specify an enumeration method other than #each : Kernel#enum_for(method = :each, *args) But #enum_for and friends always construct plain (non-lazy) enumerators, never Enumerator::Lazy . I see that Enumerator in Ruby 1.9.3 offers this similar form of #new: Enumerator#new(obj, method = :each, *args)

Python: how to do lazy debug logging

社会主义新天地 提交于 2019-11-30 04:57:45
问题 I have some python like this: def foo(): logger = logging.getLogger() # do something here logger.debug('blah blah {}'.format(expensive_func())) foo() where expensive_func() is a function that returns string and that it is expensive to execute. When developping, the log level is set to DEBUG, and expensive_func() get executed, the message get logged, everything is fine. The problem is that when I set the log level strictly greater than DEBUG, say WARNING, in production env, obviously the