CrazySoftware2014. 10. 10. 18:59

## 이해안되는 스칼라 - 1

fpis 책의 연습문제 3.13번의 해답중에 아래와 같은게 있다.

진짜 도저히 모르겠음. 이거 무슨 소린지...

/*

The implementation of `foldRight` in terms of `reverse` and `foldLeft` is a common trick for avoiding stack overflows when implementing a strict `foldRight` function as we've done in this chapter. (We'll revisit this in a later chapter, when we discuss laziness).

The other implementations build up a chain of functions which, when called, results in the operations being performed with the correct associativity. We are calling `foldRight` with the `B` type being instantiated to `B => B`, then calling the built up function with the `z` argument. Try expanding the definitions by substituting equals for equals using a simple example, like `foldLeft(List(1,2,3), 0)(_ + _)` if this isn't clear. Note these implementations are more of theoretical interest - they aren't stack-safe and won't work for large lists.

*/

def foldRightViaFoldLeft[A,B](l: List[A], z: B)(f: (A,B) => B): B =

foldLeft(reverse(l), z)((b,a) => f(a,b))

def foldRightViaFoldLeft_1[A,B](l: List[A], z: B)(f: (A,B) => B): B =

foldLeft(l, (b:B) => b)((g,a) => b => g(f(a,b)))(z)