【发布时间】:2012-06-26 18:40:19
【问题描述】:
Learn You a Haskell的第6章,介绍了如下函数:
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
作者给出了几个我觉得很容易理解的例子。然后这个:
ghci> zipWith' (zipWith' (*)) [[1,2,3],[3,5,6],[2,3,4]] [[3,2,2],[3,4,5],[5,4,3]]
哪个输出[[3,4,6],[9,20,30],[10,12,12]]
这是惰性求值的例子吗?我试图将 zipWith' 翻译成 Scheme(见下文)。我用“简单”的例子来工作,但不是最后一个,这让我认为 Haskell 的懒惰可能会有所作为。
(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)))))))
【问题讨论】:
-
不,这不是懒惰。它使用部分应用程序,这在Scheme中可能有点不重要(或者不是,我不太了解Scheme)。
-
我认为 Daniel Fischer 是对的。 Currying 显然可以用宏 phyast.pitt.edu/~micheles/scheme/scheme14.html 来模拟
-
@jberryman:我认为该链接正是我所需要的。如果您将此作为答案发布,我将很高兴为您服务!
-
@Planarian:你不需要任何宏。手动做部分应用并不难
(lambda (x y) (zipWith * x y))
标签: haskell scheme lazy-evaluation higher-order-functions partial-application