lazy-evaluation

help understand strange behavior in a lazy code

...

Lazy Evaluation in Bash

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? ...

Why don't lazy languages support mutation?

I'm studying programming language theory and I can't figure out a solid reason why lazy languages don't have mutation. Anyone know the reason? ...

Whats the point of lazy-seq in clojure?

I am looking through some example Fibonacci sequence clojure code: (def fibs (lazy-cat [1 2] (map + fibs (rest fibs)))) I generally understand what is going on, but don't get the point of lazy-cat. I know that lazy-cat is a macro that is translating to something like this: (def fibs (concat (lazy-seq [1 2]) (lazy-seq (map + fibs ...

Any way to define getters for lazy variables in Javascript arrays?

I'm trying to add elements to an array that are lazy-evaluated. This means that the value for them will not be calculated or known until they are accessed. This is like a previous question I asked but for objects. What I ended up doing for objects was Object.prototype.lazy = function(var_name, value_function) { this.__defineGetter__(...

Recursive stream throws StackOverflowError

I am defining a stream in terms of itself (a recursive definition). When trying to access the second element of the stream, StackOverflowError is thrown. The code from scala console: scala> val s1 = Stream.iterate(1)(identity _) s1: scala.collection.immutable.Stream[Int] = Stream(1, ?) scala> lazy val s2 : Stream[Int]= Stream.cons(1, (...

What is "Call By Name"?

Hi to everyone! I'm working in a homework, and the professor asked me to implement the evaluation strategy called "call by name" in scheme in a certain language that we developed and he gave us an example at http://www.scala-lang.org/node/138 in the scala language, but i don't understand in what consists the call by name evaluation stra...

Perl, evaluate string lazily

Consider the following Perl code. #!/usr/bin/perl use strict; use warnings; $b="1"; my $a="${b}"; $b="2"; print $a; The script obviously outputs 1. I would like it to be whatever the current value of $b is. What would be the smartest way in Perl to achieve lazy evaluation like this? I would like the ${b} to remain "unreplaced" ...

Haskell lazy I/O and closing files

I've written a small Haskell program to print the MD5 checksums of all files in the current directory (searched recursively). Basically a Haskell version of md5deep. All is fine and dandy except if the current directory has a very large number of files, in which case I get an error like: <program>: <currentFile>: openBinaryFile: resourc...

Truly declarative language?

Hi all, Does anyone know of a truly declarative language? The behaviour I'm looking for is kind of what Excel does, where I can define variables and formulas, and have the formula's result change when the input changes (without having set the answer again myself) The behaviour I'm looking for is best shown with this pseudo code: X = ...

Lazy Sequences that "Look Ahead" for Project Euler Problem 14

I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I may be trying to do the impossible: create a lazy sequence that is both lazy, yet also somehow 'looks ahead' for values it hasn't computed yet. The non-lazy version I wrote to test correctness was: (defn chain-length [num] (loop [len 1 n num] (con...

What language is smart so that it could understand 'variable a = 0 , 20, ..., 300' ?

What language is smart so that it could understand 'variable a = 0 , 20, ..., 300' ? so you could easily create arrays with it giving step start var last var (or, better no last variable (a la infinite array)) and not only for numbers (but even complex numbers and custom structures like Sedenion's which you would probably define on your...

What's the (hidden) cost of lazy val? (Scala)

One handy feature of Scala is lazy val, where the evaluation of a val is delayed until it's necessary (at first access). Ofcourse a lazy val must have some overhead - somewhere Scala must keep track of whether the value has already been evaluated and the evaluation must be synchronized, because multiple threads might try to access the v...

Why does this Haskell statement not evaluate lazily?

I have the following function defined: ex 1 x = 1 ex 0 x = 0 ex b x = b ** x Then, when I execute the following: 1 `ex` (sum [1..]) it tries to calculate the sum of the infinite sequence, instead of being lazy and returning 1. Why? EDIT: Upon further investigation, I found that laziness happens if I define the ex function in a ...

PHP short circuit lazy evaluation, where is it in the php.net manual?

Sorry if this sounds like a really silly question. But I Googled the web and also Googled specifically both the php.net site and the stackoverflow.com site. I know PHP does short circuit lazy evaluation when using and, or, &&, || operators, but where is it stated loud and clear in the PHP manual??? I found only Wikipedia as the only 't...

Fixed point combinators for functions over custom types?

Most examples of the use of fixed point combinators involve functions that take integers to integers (e.g. factorial). In many cases the fixed point of a function over the real numbers will end up being an arbitrary rational or perhaps irrational number (a famous example is the logistic map http://en.wikipedia.org/wiki/Logistic_map). In ...

How Are Lazy Sequences Implemented in Clojure?

I like Clojure. One thing that bothers me about the language is that I don't know how lazy sequences are implemented, or how they work. I know that lazy sequences only evaluate the items in the sequence that are asked for, how does it do this? What makes lazy sequences so efficient that they don't consume much stack? How come you can ...

Scala 2.8 and Map views

In 2.7 I could do the following: val lazyM: Map[_, _] = map.projection.mapElements(v => expCalc(v)) //MAP VIEW I can't find a way of doing this in 2.8 and actually ending up with a map: val m: Map[_, _] = map.view.map(kv => kv._1 -> expCalc(kv._2)).toMap //STRICT This seems like a significant loss of functionality and therefore I a...

Is Haskell's mapM not lazy?

UPDATE: Okay this question becomes potentially very straightforward. q <- mapM return [1..] Why does this never return? Does mapM not lazily deal with infinite lists? The code below hangs. However, if I replace line A by line B, it doesn't hang anymore. Alternatively, if I preceed line A by a "splitRandom $", it also doesn't hang...

Force all nhibernate proxies to be load for an instance...

I want to take a snapshot of an instance of an entity and therefore I want to force all proxies to load for this instance. I don't want disable lazy loading for the majority of the time I just wondering if there is an API I can call to force all proxies to be loaded. ...