Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chap 5 scanRight solution appears to calculate partial values multiple times #334

Closed
jeremystone opened this issue Nov 3, 2014 · 3 comments

Comments

@jeremystone
Copy link

With the given solution for scanRight (for exercise 5.16, p77):

Stream(1, 2, 3).scanRight(0) { (a, b) => val x = a + b; print(s"$x "); x }.toList
//> 3 5 6 3 5 3 res55: List[Int] = List(6, 5, 3, 0)

(Looks quadratic to me. Same thing for Stream(1,2,3,4,5) has 15 partial sums calculated with this implementation.)

An alternative that does not touch the by-name accumulator, p, twice per fold, namely:
def scanRight2[B](z: B)(f: (A, => B) => B): Stream[B] =
foldRight((z, Stream(z)))((a, p) => {
val (p1, p2) = p
val b2 = f(a, p1)
(b2, cons(b2, p2))
})._2

results in:
Stream(1, 2, 3).scanRight2(0) { (a, b) => val x = a + b; print(s"$x "); x }.toList
//> 3 5 6 res55: List[Int] = List(6, 5, 3, 0)

@pchiusano
Copy link
Contributor

I would actually just store the results in a lazy val. You don't want to strictly evaluate p I don't think. So something like:

(a,p0) => { lazy val p = p0; /* as before */ }

PR welcome! Also, is the numbering off on this exercise? The answer key has this as exercise 17.

@jeremystone
Copy link
Author

Yes - lazy val even better.

Looks like implementing hasSubsequence used to be exercise 5.16. This implementation is now provided in the book with scanRight taking its number.

jeremystone pushed a commit to jeremystone/fpinscala that referenced this issue Nov 5, 2014
pchiusano added a commit that referenced this issue Dec 15, 2014
@pchiusano
Copy link
Contributor

Fixed via your PR

lanocci added a commit to lanocci/fpinscala that referenced this issue Jun 3, 2020
lanocci added a commit to lanocci/fpinscala that referenced this issue Jun 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants