scheme - laziness in action? (Haskell) -
in chapter 6 of learn haskell, following function introduced:
zipwith' :: (a -> b -> c) -> [a] -> [b] -> [c] zipwith' _ [] _ = [] zipwith' _ _ [] = [] zipwith' f (x:xs) (y:ys) = f x y : zipwith' f xs ys
the author gives couple examples of use found easy enough follow. one:
ghci> zipwith' (zipwith' (*)) [[1,2,3],[3,5,6],[2,3,4]] [[3,2,2],[3,4,5],[5,4,3]]
which outputs [[3,4,6],[9,20,30],[10,12,12]]
is example of lazy evaluation? tried translate zipwith' scheme (see below). got working "easy" examples, not last one, makes me think haskell's laziness might making difference.
(define zipwith (lambda (f lista listb) (cond ((null? lista) (quote ())) ((null? listb) (quote ())) (else (cons (f (car lista) (car listb)) (zipwith f (cdr lista) (cdr listb)))))))
no, although example evaluated lazily (like other function in haskell), behaviour doesn't depend on that. on finite lists behave same way eager evaluation. on infinite lists, of course, never terminate eager evaluation, lazy evaluation allows evaluate many list elements need.
if post code you're using call scheme zipwith last example, maybe can see why that's behaving differently.
Comments
Post a Comment