lazy-evaluation

Forced strictness for lists in haskell

ぃ、小莉子 提交于 2019-12-05 00:54:19
I made really time consuming algorithm which produces a short string as the result. When I try to print it (via putStrLn) it appears on the screen character by character. I did understand why that happened, and I tried to force evaluation of the string before actual printing. myPrint !str = putStrLn str But this help very little. When I ran the program in debug I noticed that the !str forced evaluation only for the first character. Does anyone know why is that, and how to deal with this? (!) translates into seq , which evaluates strictly to Weak Head Normal Form -- that is, it only evaluates

F# PSeq.iter does not seem to be using all cores

。_饼干妹妹 提交于 2019-12-05 00:45:17
I've been doing some computationally intensive work in F#. Functions like Array.Parallel.map which use the .Net Task Parallel Library have sped up my code exponentially for a really quite minimal effort. However, due to memory concerns, I remade a section of my code so that it can be lazily evaluated inside a sequence expression (this means I have to store and pass less information). When it came time to evaluate I used: // processor and memory intensive task, results are not stored let calculations : seq<Calculation> = seq { ...yield one thing at a time... } // extract results from

lazy evaluation and late binding of python?

[亡魂溺海] 提交于 2019-12-04 23:51:56
问题 when is lazy evaluation? (generator, if, iterator?), when is late binding? (closure, regular functions?) a = [1,2,3,4] b = [lambda y: x for x in a] c = (lambda y: x for x in a) #lazy evaluation d = map(lambda m: lambda y:m, a) #closure for i in b: print i(None) # 4 4 4 4 for i in c: print i(None) # 1 2 3 4 for i in d: print i(None) # 1 2 3 4 回答1: This looks like homework, so I won't just give you the answers. Here are two functions that you can step through and see how the values change. def

Idris eager evaluation

瘦欲@ 提交于 2019-12-04 22:17:06
In Haskell , I might implement if like this: if' True x y = x if' False x y = y spin 0 = () spin n = spin (n - 1) This behaves how I expect : haskell> if' True (spin 1000000) () -- takes a moment haskell> if' False (spin 1000000) () -- immediate In Racket , I could implement a flawed if like this: (define (if2 cond x y) (if cond x y)) (define (spin n) (if (= n 0) (void) (spin (- n 1)))) This behaves how I expect : racket> (if2 #t (spin 100000000) (void)) -- takes a moment racket> (if2 #f (spin 100000000) (void)) -- takes a moment In Idris , I might implement if like this: if' : Bool -> a -> a

How is Haskell's 'seq' different from other functions?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 21:12:38
问题 I'm confused about the description of how Haskell's seq works in a tutorial I'm reading. The tutorial states that evaluating the expression seq x y will first evaluate x to WHNF and only then continue with the evaluation of y But earlier the same tutorial, in explaining how Haskell's lazy evaluation works in general, states that in evaluating a function, argues are "evaluated, but only as far as necessary" which means that its arguments will be evaluated from left to right until their topmost

Do inner bang patterns always force outer constructors in Haskell?

一笑奈何 提交于 2019-12-04 18:55:21
问题 In Haskell, is there ever a situation where for a data type {-# LANGUAGE BangPatterns #-} import Control.DeepSeq data D = D Int the instance instance NFData D where rnf (D !_) = () can have a different effect than the instance with another outer ! : instance NFData D where rnf !(D !_) = () My research: https://downloads.haskell.org/~ghc/8.6.3/docs/html/users_guide/glasgow_exts.html#bang-patterns-informal only talks about let bindings (like this answer), which I think doesn't apply for

What is Lazy Binary Search?

删除回忆录丶 提交于 2019-12-04 18:22:05
I don't know whether the term "Lazy" Binary Search is valid, but I was going through some old materials and I just wanted to know if anyone can explain the algorithm of a Lazy Binary Search and compare it to a non-lazy Binary Search. Let's say, we have this array of numbers: 2, 11, 13, 21, 44, 50, 69, 88 How to look for the number 11 using a Lazy Binary Search? As far as I am aware "Lazy binary search" is just another name for " Binary search ". Justin was wrong. The common binarySearch algorithm first checks whether the target is equal to current middle entry before proceeding to the left or

A variable used in its own definition?

筅森魡賤 提交于 2019-12-04 17:45:51
问题 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. 回答1: 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

Time cost of Haskell `seq` operator

戏子无情 提交于 2019-12-04 17:36:35
问题 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")? 回答1: The seq function will discard the value of x , but since the value has been evaluated, all

Is it possible to match with decomposed sequences in F#?

六眼飞鱼酱① 提交于 2019-12-04 16:21:57
问题 I seem to remember an older version of F# allowing structural decomposition when matching sequences just like lists. Is there a way to use the list syntax while keeping the sequence lazy? I'm hoping to avoid a lot of calls to Seq.head and Seq.skip 1. I'm hoping for something like: let decomposable (xs:seq<'a>) = match xs with | h :: t -> true | _ -> false seq{ 1..100 } |> decomposable But this only handles lists and gives a type error when using sequences. When using List.of_seq, it seems to