code in scala def unfold[A, S](z: S)(f: S => Option[(A, S)]): LazyList[A] = f(z) match { case Some((h, s)) => h #:: unfold(s)(f) case None => LazyList() }
code in scala
def unfold[A, S](z: S)(f: S => Option[(A, S)]): LazyList[A] = f(z) match { case Some((h, s)) => h #:: unfold(s)(f) case None => LazyList() }
A positive integer is perfect if it equals the sum of all of its factors, excluding the number itself. For example, 6, 28, 496, and so on. Define a LazyList to generate an infinite list of perfect numbers. Use higher-order functions whenever possible. [Hint: One way to do this is by (1) having one function construct a list of factors (use unfold and filter for this), (2) having another function which folds the list of factors into their sum, and (3) creating a LazyList using unfold, filter and the previously constructed functions.]
e number itself. For example, 6, 28, 496, and so on. Define a LazyList to generate an infinite list of perfect
Step by step
Solved in 3 steps