Difference between revisions of "Deep Lists Assignment"
Line 4: | Line 4: | ||
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldl%29%29 foldl] | * [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldl%29%29 foldl] | ||
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldr%29%29 foldr] | * [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldr%29%29 foldr] | ||
+ | |||
+ | =Use= | ||
+ | * [https://docs.racket-lang.org/reference/num[ber-types.html#%28def._%28%28quote._~23~25kernel%29._number~3f%29%29 number?] | ||
+ | * [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29 list?] | ||
+ | * [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldl%29%29 foldl] | ||
+ | * [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldr%29%29 foldr] | ||
+ | * [https://docs.racket-lang.org/reference/generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._%2B%29%29 +] | ||
=Code To Implement= | =Code To Implement= | ||
{{RacketToImplement|deep_lists|deep-fold<br/>deep-flatten<br/>deep-sum<br/>deep-sum-numbers|deep_lists}} | {{RacketToImplement|deep_lists|deep-fold<br/>deep-flatten<br/>deep-sum<br/>deep-sum-numbers|deep_lists}} | ||
+ | ==Fold== | ||
==deep-fold== | ==deep-fold== | ||
− | ==deep-flatten== | + | <nowiki>(define (deep-fold list-fold) |
− | ==deep-sum== | + | (error 'not-yet-implemented)) |
− | ==deep-sum-numbers== | + | |
+ | (define deep-foldl (deep-fold foldl)) | ||
+ | (define deep-foldr (deep-fold foldr))</nowiki> | ||
+ | |||
+ | You can elect to build deep-fold as a higher order function which returns a function which will deep fold left or right depending on which list fold function is passed to it. | ||
+ | |||
+ | NOTE: You can also elect to just build deep-foldl and/or deep-foldr directly. If you choose to implement only one of deep-foldl and deep-foldr, be sure to comment out the tests for the unimplemented one. | ||
+ | ==Clients== | ||
+ | ===deep-flatten=== | ||
+ | Utilize a version of [[#deep-fold] (and perhaps a list [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._reverse%29%29 reverse] depending on which fold direction you choose). | ||
+ | ===deep-sum=== | ||
+ | Sums the numbers in a deep list of numbers and lists. | ||
+ | ===deep-sum-numbers=== | ||
+ | Like deep-sum only it ignores values which are neither lists nor numbers. | ||
=Test= | =Test= | ||
{{RacketUnitTest|deep_lists_test|deep_lists}} | {{RacketUnitTest|deep_lists_test|deep_lists}} |
Revision as of 14:22, 28 March 2022
original inspiration for this warmup comes from Extra Practice Problems
Contents
Code To Use
Use
Code To Implement
file: | src/main/racket/deep_lists/deep_lists.rkt | |
functions: | deep-fold deep-flatten deep-sum deep-sum-numbers |
Fold
deep-fold
(define (deep-fold list-fold) (error 'not-yet-implemented)) (define deep-foldl (deep-fold foldl)) (define deep-foldr (deep-fold foldr))
You can elect to build deep-fold as a higher order function which returns a function which will deep fold left or right depending on which list fold function is passed to it.
NOTE: You can also elect to just build deep-foldl and/or deep-foldr directly. If you choose to implement only one of deep-foldl and deep-foldr, be sure to comment out the tests for the unimplemented one.
Clients
deep-flatten
Utilize a version of [[#deep-fold] (and perhaps a list reverse depending on which fold direction you choose).
deep-sum
Sums the numbers in a deep list of numbers and lists.
deep-sum-numbers
Like deep-sum only it ignores values which are neither lists nor numbers.
Test
file: | deep_lists_test.rkt | Test |
source folder: | src/test/racket/deep_lists |
note: ensure that you have removed all printing to receive credit for any assignment.