사용 하스켈의 documentation 출발점으로 (코멘트에 @soegaard에서 언급 한 바와 같이), 여기에 사용 라켓 구문이 문제에 대한 작업 솔루션입니다 : 예를 들어
(define (foldl/w/foldr f base lst)
((foldr (λ (ele acc) (λ (x) (acc (f ele x))))
identity
lst)
base))
:
(foldl/w/foldr cons '() '(1 2 3 4 5))
=> '(5 4 3 2 1)
(foldl/w/foldr + 0 '(1 2 3 4 5))
=> 15
이것을 이해하는 열쇠는 값이 아닌 지연 계산을 사용하여 람다을 누적하는 것이며 결국에는 계산을 시작하기 위해 기본 값을 전달하는 모든 람다 체인을 호출합니다. 또한 identity
절차가 첫 번째 누산기로 사용되며 더 많은 램버스가 누적됩니다. 예를 들어,이 호출 :
(foldl/w/foldr + 0 '(1 2))
로 평가 될 것인가는 다음과
((lambda (x) ; this lambda is the value returned by foldr
((lambda (x)
(identity (+ 1 x))) ; add first element in the list (this gets executed last)
(+ 2 x))) ; add second element in the list (this gets executed first)
0) ; at the end, we invoke the chain of lambdas starting with the base value
=> 3
하스켈 : https://wiki.haskell.org/Foldl_as_foldr – soegaard