lazy-evaluation

lazy list computed using mutable state?

断了今生、忘了曾经 提交于 2019-12-10 05:24:34
问题 I'd like to figure out in general how to use mutable state in the computation of lazy lists. For instance, here is a naive Sieve of Eratosthenes implemented using a mutable array (source): import Control.Monad.ST import Data.Array.ST import Data.Array.Unboxed import Control.Monad import Data.List prime :: Int -> UArray Int Bool prime n = runSTUArray $ do arr <- newArray ( 2 , n ) True :: ST s ( STUArray s Int Bool ) forM_ ( takeWhile ( \x -> x*x <= n ) [ 2 .. n ] ) $ \i -> do ai <- readArray

Is equality testing possible between two infinite data structure in Haskell?

Deadly 提交于 2019-12-10 03:43:59
问题 In a project I'm working on, data of a certain type may sometimes contain themselves in it. For example, data Example = Apple Int | Pear Int Example a = Pear 10 a b = Pear 10 b As a programmer I know that a and b are equal, but when I actually test equality between them it would infinite loop because their values need to be evaluated for comparison. Is there any other way to do equality testing between data like these? Or is there a way to avoid problems like these? 回答1: In general: no. This

Ocaml: Lazy Lists

不问归期 提交于 2019-12-10 03:26:21
问题 How can I make a lazy list representing a sequence of doubling numbers? Example: 1 2 4 8 16 32 回答1: Using streams: let f x = Stream.from (fun n -> Some (x * int_of_float (2.0 ** float_of_int n))) or let f x = let next = ref x in Stream.from (fun _ -> let y = !next in next := 2 * y ; Some y) Using a custom lazy_list type: type 'a lazy_list = | Nil | Cons of 'a * 'a lazy_list lazy_t let rec f x = lazy (Cons (x, f (2*x))) 回答2: The great blog enfranchised mind has a great article on this topic:

Lazy evaluation with ostream C++ operators

泄露秘密 提交于 2019-12-10 03:09:19
问题 I am looking for a portable way to implement lazy evaluation in C++ for logging class. Let's say that I have a simple logging function like void syslog(int priority, const char *format, ...); then in syslog() function we can do: if (priority < current_priority) return; so we never actually call the formatting function (sprintf). On the other hand, if we use logging stream like log << LOG_NOTICE << "test " << 123; all the formating is always executed, which may take a lot of time. Is there any

F# Lazy Evaluation vs Non-Lazy

强颜欢笑 提交于 2019-12-10 02:40:00
问题 I'm just beginning F# so please be kind if this is basic. I've read that a function marked lazy is evaluated only once and then cached. For example: let lazyFunc = lazy (1 + 1) let theValue = Lazy.force lazyFunc Compared to this version which would actually run each time it's called: let eagerFunc = (1 + 1) let theValue = eagerFunc Based on that, should all functions be made lazy? When would you not want to? This is coming from material in the book "Beginning F#" . 回答1: First of all, it may

Forced strictness for lists in haskell

喜你入骨 提交于 2019-12-10 01:32:12
问题 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? 回答1:

What is Lazy Binary Search?

淺唱寂寞╮ 提交于 2019-12-09 23:59:28
问题 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? 回答1: As far as I am aware "Lazy binary search" is just another name for "Binary search". 回答2: Justin was wrong. The common

How to add a classmethod in Python dynamically

我与影子孤独终老i 提交于 2019-12-09 17:38:47
问题 I'm using Python 3. I know about the @classmethod decorator. Also, I know that classmethods can be called from instances. class HappyClass(object): @classmethod def say_hello(): print('hello') HappyClass.say_hello() # hello HappyClass().say_hello() # hello However, I don't seem to be able to create class methods dynamically AND let them be called from instances. Let's say I want something like class SadClass(object): def __init__(self, *args, **kwargs): # create a class method say_dynamic

Lazily transpose a list in Python

﹥>﹥吖頭↗ 提交于 2019-12-09 15:01:29
问题 So, I have an iterable of 3-tuples, generated lazily. I'm trying to figure out how to turn this into 3 iterables, consisting of the first, second, and third elements of the tuples, respectively. However, I wish this to be done lazily. So, for example, I wish [(1, 2, 3), (4, 5, 6), (7, 8, 9)] to be turned into [1, 4, 7] , [2, 5, 8] , [3, 6, 9] . (Except I want iterables not lists.) The standard zip(*data) idiom doesn't work, because the argument unpacking expands the entire iterable. (You can

Evaluation and space leaks in Haskell

狂风中的少年 提交于 2019-12-09 09:04:00
问题 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 =