lazy-evaluation

Playing with infinity - Lazy arithmetics

眉间皱痕 提交于 2019-12-05 07:13:57
Many modern programming languages allow us to handle potentially infinite lists and to perform certain operations on them. Example [Python]: EvenSquareNumbers = ( x * x for x in naturals() if x mod 2 == 0 ) Such lists can exist because only elements that are actually required are computed. (Lazy evaluation) I just wondered out of interest whether it's possible (or even practised in certain languages) to extend the mechanism of lazy evaluation to arithmetics. Example: Given the infinite list of even numbers evens = [ x | x <- [1..], even x ] We couldn't compute length evens since the

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

纵饮孤独 提交于 2019-12-05 04:45:00
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? In general: no. This sort of equality testing reduces to the halting problem. One way to see this is that we could express the

How do I write a constant-space length function in Haskell?

╄→гoц情女王★ 提交于 2019-12-05 04:32:48
The canonical implementation of length :: [a] -> Int is: length [] = 0 length (x:xs) = 1 + length xs which is very beautiful but suffers from stack overflow as it uses linear space. The tail-recursive version: length xs = length' xs 0 where length' [] n = n length' (x:xs) n = length xs (n + 1) doesn't suffer from this problem, but I don't understand how this can run in constant space in a lazy language. Isn't the runtime accumulating numerous (n + 1) thunks as it moves through the list? Shouldn't this function Haskell to consume O(n) space and lead to stack overflow? (if it matters, I'm using

In Haskell, will calling length on a Lazy ByteString force the entire string into memory?

拥有回忆 提交于 2019-12-05 03:41:44
I am reading a large data stream using lazy bytestrings, and want to know if at least X more bytes is available while parsing it. That is, I want to know if the bytestring is at least X bytes long. Will calling length on it result in the entire stream getting loaded, hence defeating the purpose of using the lazy bytestring? If yes, then the followup would be: How to tell if it has at least X bytes without loading the entire stream? EDIT: Originally I asked in the context of reading files but understand that there are better ways to determine filesize. Te ultimate solution I need however should

Ocaml: Lazy Lists

余生长醉 提交于 2019-12-05 03:34:28
How can I make a lazy list representing a sequence of doubling numbers? Example: 1 2 4 8 16 32 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))) chollida The great blog enfranchised mind has a great article on this topic: http://enfranchisedmind.com/blog/posts/ocaml-lazy-lists-an-introduction/ You can also check out http:/

Lazy vs eager evaluation and double linked list building

匆匆过客 提交于 2019-12-05 03:26:09
I can't sleep! :) I've written small program building double linked list in Haskell. The basic language's property to make it was lazy evaluation (see the bunch of code below). And my question is can I do the same in a pure functional language with eager evaluation or not? In any case, what properties eager functional language must have to be able to build such structure (impurity?)? import Data.List data DLList a = DLNull | DLNode { prev :: DLList a , x :: a , next :: DLList a } deriving (Show) walkDLList :: (DLList a -> DLList a) -> DLList a -> [a] walkDLList _ DLNull = [] walkDLList f n@

How does this list comprehension over the inits of itself work?

与世无争的帅哥 提交于 2019-12-05 02:45:30
In the #haskell IRC channel someone asked Is there a succinct way to define a list where the nth entry is the sum of the squares of all entries before? I thought this sounded like a fun puzzle, and defining infinite lists recursively is one of those things I really need to practise. So I fired up GHCi and started playing around with recursive definitions. Eventually, I managed to get to λ> let xs = 1 : [sum (map (^2) ys) | ys <- inits xs, not (null ys)] which seems to produce the correct results: λ> take 9 xs [1,1,2,6,42,1806,3263442,10650056950806,113423713055421844361000442] Unfortunately, I

F# Lazy Evaluation vs Non-Lazy

浪子不回头ぞ 提交于 2019-12-05 01:47:48
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#" . First of all, it may be helpful to note that none of the things you have defined is a function - eagerFunc and theValue are values

hibernate, to be lazy or not to be lazy? [closed]

▼魔方 西西 提交于 2019-12-05 01:32:30
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I have entity A , which has a many-to-many relation to entity B . So the table layout is : A, AB(mapping table), B To get an object of entity A: I call A.getById() which does getHibernateTemplate().get(A.class, id) using spring and hibernate. The problem is, sometimes ensuing

How do laziness and I/O work together in Haskell?

纵饮孤独 提交于 2019-12-05 01:20:32
I'm trying to get a deeper understanding of laziness in Haskell. I was imagining the following snippet today: data Image = Image { name :: String, pixels :: String } image :: String -> IO Image image path = Image path <$> readFile path The appeal here is that I could simply create an Image instance and pass it around; if I need the image data it would be read lazily - if not, the time and memory cost of reading the file would be avoided: main = do image <- image "file" putStrLn $ length $ pixels image But is that how it actually works? How is laziness compatible with IO? Will readFile be