lazy-evaluation

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

一曲冷凌霜 提交于 2019-12-18 14:51:51
问题 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

Scala case class prohibits call-by-name parameters?

青春壹個敷衍的年華 提交于 2019-12-18 12:47:38
问题 Scenario: I want to implement an infinite list: abstract class MyList[+T] case object MyNil extends MyList[Nothing] case class MyNode[T](h:T,t: => MyList[T]) extends MyList[T] //error: `val' parameters may not be call-by-name Problem: The error is that call-by-name is not allowed. I've heard that it is because val or var constructor parameter is not allowed for call-by-name . For example: class A(val x: =>Int) //error: `val' parameters may not be call-by-name But in contrast the normal

Sequence vs LazyList

心不动则不痛 提交于 2019-12-18 12:15:31
问题 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 ? 回答1: LazyList computes each element only once regardless of

Can a thunk be duplicated to improve memory performance?

三世轮回 提交于 2019-12-18 12:08:10
问题 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

Lazy Evaluation vs Macros

依然范特西╮ 提交于 2019-12-18 10:05:22
问题 I'm used to lazy evaluation from Haskell, and find myself getting irritated with eager-by-default languages now that I've used lazy evaluation properly. This is actually quite damaging, as the other languages I use mainly make lazily evaluating stuff very awkward, normally involving the rolling out of custom iterators and so forth. So just by acquiring some knowledge, I've actually made myself less productive in my original languages. Sigh. But I hear that AST macros offer another clean way

Need to force realization of lazy seqs before/after element-wise imperative operations?

可紊 提交于 2019-12-18 09:36:52
问题 If I perform a side-effecting/mutating operation on individual data structures specific to each member of lazy sequence using map , do I need to (a) call doall first, to force realization of the original sequence before performing the imperative operations, or (b) call doall to force the side-effects to occur before I map a functional operation over the resulting sequence? I believe that no doall s are necessary when there are no dependencies between elements of any sequence, since map can't

strange jags.parallel error / avoiding lazy evaluation in function call

ⅰ亾dé卋堺 提交于 2019-12-18 08:57:40
问题 I have a function call (to jags.parallel ) that works when given a numerical argument like n.iter = 100 but fails when the argument uses a variable value, n.iter = n.iter . This looks like it might be a bug in jags.parallel A minimal reproducible example of the error: library(R2jags) model.file <- system.file(package="R2jags", "model", "schools.txt") J <- 8.0 y <- c(28.4,7.9,-2.8,6.8,-0.6,0.6,18.0,12.2) sd <- c(14.9,10.2,16.3,11.0,9.4,11.4,10.4,17.6) jags.data <- list("y","sd","J") jags

Lazy Evaluation: Why is it faster, advantages vs disadvantages, mechanics (why it uses less cpu; examples?) and simple proof of concept examples [closed]

百般思念 提交于 2019-12-18 05:07:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Lazy evaluation is said to be a way of delaying a process until the first time it is needed. This tends to avoid repeated evaluations and thats why I would imagine that is performing a lot faster. Functional language like Haskell (and JavaScript..?) have this functionality built

good practice in c++ (lazy evaluation)

佐手、 提交于 2019-12-18 04:53:26
问题 I have some question about lazy evaluation of c++, can I be sure that this snippet of the code will always work, or it is bad idea? if Yes, why? Thanks in advance if(currentNode == 0 || *currentNode == element){ return; } 回答1: It is guaranteed to work: logical AND and OR expression chains are evaluated from left to right, and if the first subexpression satisfies the condition, no further subexpressions are evaluated. In your case, if currentNode is null, it will never get dereferenced by the

Lazy Evaluation in Bash

时光怂恿深爱的人放手 提交于 2019-12-18 04:00:11
问题 Is there more elegant way of doing lazy evaluation than the following: pattern='$x and $y' x=1 y=2 eval "echo $pattern" results: 1 and 2 It works but eval "echo ..." just feels sloppy and may be insecure in some way. Is there a better way to do this in Bash? 回答1: You can use the command envsubst from gettext, for example: $ pattern='x=$x and y=$y' $ x=1 y=2 envsubst <<< $pattern x=1 and y=2 回答2: One safe possibility is to use a function: expand_pattern() { pattern="$x and $y" } That's all.