lazy-evaluation

How many ways are there to describe the Fibonacci sequence in Perl 6?

馋奶兔 提交于 2019-12-02 18:13:20
I've been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence. I will start this off with the three from masak 's journal: my @fibs := (0, 1, -> $a, $b { $a + $b } ... *); my @fibs := (0, 1, { $^a + $^b } ... *); my @fibs := (0, 1, *+* ... *); I was thinking something like this would also work, but I think I have the syntax wrong: my @fibs := (0, 1, (@fibs Z+ @fibs[1..*])); Something there is eager (the slice?) and causes Rakudo to enter an infinite loop. It's a translation of the Haskell

Efficient table for Dynamic Programming in Haskell

≡放荡痞女 提交于 2019-12-02 17:46:46
I've coded up the 0-1 Knapsack problem in Haskell. I'm fairly proud about the laziness and level of generality achieved so far. I start by providing functions for creating and dealing with a lazy 2d matrix. mkList f = map f [0..] mkTable f = mkList (\i -> mkList (\j -> f i j)) tableIndex table i j = table !! i !! j I then make a specific table for a given knapsack problem knapsackTable = mkTable f where f 0 _ = 0 f _ 0 = 0 f i j | ws!!i > j = leaveI | otherwise = max takeI leaveI where takeI = tableIndex knapsackTable (i-1) (j-(ws!!i)) + vs!!i leaveI = tableIndex knapsackTable (i-1) j --

What is spine strictness

不打扰是莪最后的温柔 提交于 2019-12-02 17:33:34
In Haskell, the term spine strictness is often mentioned in relation to lazy evaluation. Though I have a vague understanding of that it means, it would be nice to have a more concrete explanation about: What is the spine of a data structure What does spine strictness mean? What are the benefits when comparing spine strict data structures with lazy ones? Here's an example > length (undefined : 3 : 4 : undefined : []) 4 > length (2 : 3 : 4 : 5 : undefined) <<loop>> The first list contains bottoms as elements, but the "shape" of the list is fully defined. Roughly speaking, every list cell has a

Accessing a non-static member via Lazy<T> or any lambda expression

Deadly 提交于 2019-12-02 17:24:37
I have this code: public class MyClass { public int X { get; set; } public int Y { get; set; } private Lazy<int> lazyGetSum = new Lazy<int>(new Func<int>(() => X + Y)); public int Sum{ get { return lazyGetSum.Value; } } } Gives me this error: A field initializer cannot reference the non-static field, method, or property. I think it is very reasonable to access a non-static member via lazy, how to do this? * EDIT * The accepted answer solves the problem perfectly, but to see the detailed and in-depth -as always- reason of the problem you can read Joh Skeet's answer . JleruOHeP You can move it

Disadvantages of Lazy<T>?

回眸只為那壹抹淺笑 提交于 2019-12-02 17:24:35
I recently started using Lazy throughout my application, and I was wondering if there are any obvious negative aspects that I need to take into account when using Lazy<T> ? I am trying to utilize Lazy<T> as often as I deem it appropriate, primarily to help reduce the memory footprint of our loaded, but inactive plugins. I'll expand a bit on my comment, which reads: I've just started using Lazy, and find that it's often indicative of bad design; or laziness on the part of the programmer. Also, one disadvantage is that you have to be more vigilant with scoped up variables, and create proper

Lazy List of Prime Numbers

荒凉一梦 提交于 2019-12-02 16:41:15
How would one implement a list of prime numbers in Haskell so that they could be retrieved lazily? I am new to Haskell, and would like to learn about practical uses of the lazy evaluation functionality. Niki Here's a short Haskell function that enumerates primes from Literate Programs: primes :: [Integer] primes = sieve (2 : [3, 5..]) where sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p > 0] Apparently, this is not the Sieve of Eratosthenes (thanks, Landei). I think it's still an instructive example that shows you can write very elegant, short code in Haskell and that shows how the choice of

What are Haskell's strictness points?

半腔热情 提交于 2019-12-02 15:45:29
We all know (or should know) that Haskell is lazy by default. Nothing is evaluated until it must be evaluated. So when must something be evaluated? There are points where Haskell must be strict. I call these "strictness points", although this particular term isn't as widespread as I had thought. According to me: Reduction (or evaluation) in Haskell only occurs at strictness points. So the question is: what, precisely , are Haskell's strictness points? My intuition says that main , seq / bang patterns, pattern matching, and any IO action performed via main are the primary strictness points, but

How is debugging achieved in a lazy functional programming language?

我的未来我决定 提交于 2019-12-02 15:13:40
I'd like to know how debugging is achieved in a lazy functional language. Can you use breakpoints, print statements and traditional techniques? Is this even a good idea? It is my understanding that pure functional programming does not allow side-effects, with the exception of monads. Order of execution is also not guaranteed. Would you have to program a monad for every section of code you want to test? I'd like some insight into this question from someone more experienced in this area. Nothing prevents you from using breakpoints in a lazily evaluated functional program. The difference to eager

Haskell: How does non-strict and lazy differ?

南楼画角 提交于 2019-12-02 14:04:07
I often read that lazy is not the same as non-strict but I find it hard to understand the difference. They seem to be used interchangeably but I understand that they have different meanings. I would appreciate some help understanding the difference. I have a few questions which are scattered about this post. I will summarize those questions at the end of this post. I have a few example snippets, I did not test them, I only presented them as concepts. I have added quotes to save you from looking them up. Maybe it will help someone later on with the same question. Non-Strict Def: A function f is

Scala Lazy Val Question

牧云@^-^@ 提交于 2019-12-02 13:18:29
问题 I have a scenario where I have some objects that need to take in references from each other. The only way I can get this to compile is to use lazy class A(b:B) class B(a:A) lazy val a:A = new A(b) lazy val b:B = new B(a) I can do the same thing using some actors, and get it to compile also abstract class Message case class Message1 extends Message case class Message2 extends Message class Actor1(otherActor:Actor) extends Actor { def act() { loop { react { case Message1 => println("received