lazy-evaluation

Lazy “n choose k” in OCaml

試著忘記壹切 提交于 2019-12-03 12:59:54
问题 As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list is not relevant. For example, choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]] Any ideas? I would like to have the whole thing to be lazy, outputting a lazy

Observing lazyness in Haskell

≡放荡痞女 提交于 2019-12-03 12:48:22
Is it possible to write a Haskell function which depends on whether values are calculated already or are thunks? E.g. if lazyShow :: [Int] -> String shows thunks as ? and calculated values normally, in GHCi we would see > let nats = [0..] > lazyShow nats 0 : ? > nats !! 5 5 > lazyShow nats 0 : 1 : 2 : 3 : 4 : ? Clearly, lazyShow cannot have the type you state. If the string should depend on the current state of evaluation, then IO String as a result is the best you can hope for. If all you're interested in is using this for debugging, then I think that the ghc-heap-view package (and

How to understand clojure's lazy-seq

寵の児 提交于 2019-12-03 12:45:36
问题 I'm trying to understand clojure's lazy-seq operator, and the concept of lazy evaluation in general. I know the basic idea behind the concept: Evaluation of an expression is delayed until the value is needed. In general, this is achievable in two ways: at compile time using macros or special forms; at runtime using lambda functions With lazy evaluation techniques, it is possible to construct infinite data structures that are evaluated as consumed. These infinite sequences utilizes lambdas,

Lazy evaluation

扶醉桌前 提交于 2019-12-03 12:30:03
问题 How can I lazy evaluate second arg in std::conditional? #include "stdafx.h" #include <type_traits> struct Null{}; struct _1{enum {one = true,two = false};}; struct _2{enum {two = true, one = false};}; template<class T> struct is_nulltype { enum {value = false}; }; template<> struct is_nulltype<Null> { enum {value = true}; }; template<class T> struct X : std::conditional<is_nulltype<T>::value,Null,typename std::conditional<T::one,_1,_2>::type>::type { }; int _tmain(int argc, _TCHAR* argv[]) {

A variable used in its own definition?

我们两清 提交于 2019-12-03 12:27:21
An infinite stream: val ones: Stream[Int] = Stream.cons(1, ones) How is it possible for a value to be used in its own declaration? It seems this should produce a compiler error, yet it works. It's not always a recursive definition. This actually works and produces 1: val a : Int = a + 1 println(a) variable a is created when you type val a: Int , so you can use it in the definition. Int is initialized to 0 by default. A class will be null. As @Chris pointed out, Stream accepts => Stream[A] so a bit another rules are applied, but I wanted to explain general case. The idea is still the same, but

Any difference between Lazy evaluation and Short-circuit evaluation?

冷暖自知 提交于 2019-12-03 12:15:58
问题 From Wikipedia: Lazy evaluation is: In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed Short-circuit evaluation is: Short-circuit evaluation, minimal evaluation, or McCarthy evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of

When exactly is the head of a Stream evaluated?

 ̄綄美尐妖づ 提交于 2019-12-03 12:00:53
Normally if you create a Stream object, the head will be eagerly evaluated: scala> Stream( {println("evaluating 1"); 1} , 2, 3) evaluating 1 res63: scala.collection.immutable.Stream[Int] = Stream(1, ?) If we create a Stream to which we prepend in the same statement, it seems slightly surprising that the head is not evaluated prior to the concatenation. i.e. scala> 0 #:: Stream( {println("evaluating 1"); 1} , 2, 3) res65: scala.collection.immutable.Stream[Int] = Stream(0, ?) ( #:: is right-associative and is the prepend method on ConsWrapper , which is an implicit class of Stream .) How does

How to optimize this short factorial function in scala? (Creating 50000 BigInts)

时光怂恿深爱的人放手 提交于 2019-12-03 11:41:14
I've compaired the scala version (BigInt(1) to BigInt(50000)).reduce(_ * _) to the python version reduce(lambda x,y: x*y, range(1,50000)) and it turns out, that the scala version took about 10 times longer than the python version. I'm guessing, a big difference is that python can use its native long type instead of creating new BigInt-objects for each number. But is there a workaround in scala? Travis Brown The fact that your Scala code creates 50,000 BigInt objects is unlikely to be making much of a difference here. A bigger issue is the multiplication algorithm— Python's long uses Karatsuba

Evaluation and space leaks in Haskell

半城伤御伤魂 提交于 2019-12-03 11:35:15
I'm learning Haskell and currently trying to wrap my head around monads. While playing with some random number generation I got tripped on lazy evaluation once again. In an effort to simplify something close to the: roll :: State StdGen Int roll = do gen <- get let (n, newGen) = randomR (0,1) gen put newGen return n main = do gen <- getStdGen let x = sum $ evalState (replicateM iterations roll) gen print x into something like this: roll' :: IO Int roll' = getStdRandom $ randomR (0,1) main = do x' <- fmap sum $ replicateM iterations roll' print x' on a larger number of iterations , let's say

Time cost of Haskell `seq` operator

江枫思渺然 提交于 2019-12-03 11:15:56
This FAQ says that The seq operator is seq :: a -> b -> b x seq y will evaluate x, enough to check that it is not bottom, then discard the result and evaluate y. This might not seem useful, but it means that x is guaranteed to be evaluated before y is considered. That's awfully nice of Haskell, but does it mean that in x `seq` f x the cost of evaluating x will be paid twice ("discard the result")? The seq function will discard the value of x , but since the value has been evaluated, all references to x are "updated" to no longer point to the unevaluated version of x , but to instead point to