Difference between revisions of "Scan Higher Order Function Assignment"
Jump to navigation
Jump to search
(→scan) |
|||
Line 12: | Line 12: | ||
=Clients= | =Clients= | ||
− | == scan op+ == | + | ==int== |
+ | ===scan op+=== | ||
For example: | For example: | ||
Line 30: | Line 31: | ||
|} | |} | ||
− | == scan op* == | + | ==scan op*== |
For example: | For example: | ||
Line 46: | Line 47: | ||
! style="text-align:right;" | scan op* xs | ! style="text-align:right;" | scan op* xs | ||
| 131 || 30261 || 12860925 | | 131 || 30261 || 12860925 | ||
+ | |} | ||
+ | |||
+ | ==set== | ||
+ | ===scan intersection=== | ||
+ | For example: | ||
+ | |||
+ | scan intersection [fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([0,1,2]), fromList([231,425])] | ||
+ | |||
+ | would produce: | ||
+ | |||
+ | [fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([2]), empty] | ||
+ | |||
+ | :{| class="wikitable" style="text-align:right;" | ||
+ | |- | ||
+ | ! style="text-align:right;" | xs | ||
+ | | {1,2,3,4,5} || {2,3,4} || {0,1,2} || {231,425} | ||
+ | |- | ||
+ | ! style="text-align:right;" | scan op+ xs | ||
+ | | {1,2,3,4,5} || {2,3,4} || {2} || {} | ||
+ | |} | ||
+ | |||
+ | ===scan union=== | ||
+ | For example: | ||
+ | |||
+ | scan intersection [fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([0,1,2]), fromList([231,425])] | ||
+ | |||
+ | would produce: | ||
+ | |||
+ | [fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([2]), empty] | ||
+ | |||
+ | :{| class="wikitable" style="text-align:right;" | ||
+ | |- | ||
+ | ! style="text-align:right;" | xs | ||
+ | | {1,2,3,4,5} || {2,3,4} || {0,1,2} || {231,425} | ||
+ | |- | ||
+ | ! style="text-align:right;" | scan op+ xs | ||
+ | | {1,2,3,4,5} || {1,2,3,4,5} || {0,1,2,3,4,5} || {0,1,2,3,4,5,231,425} | ||
|} | |} | ||
=Test= | =Test= | ||
{{SMLUnitTest|scan|warmup_scan}} | {{SMLUnitTest|scan|warmup_scan}} |
Revision as of 09:15, 16 February 2022
Contents
Background
Previously, we have built sum_scan. This is a specific version of scan where the operation is +. We will build a higher-order function scan which takes an operation to apply, as well as a list.
Code To Implement
file: | src/main/sml/warmup_scan/scan.sml | |
functions: | scan |
val scan = fn : ('a * 'a -> 'a) -> 'a list -> 'a list
Note: this higher-order function is curried.
fun scan operation xs
Clients
int
scan op+
For example:
scan op+ [131,231,425]
would produce:
[131, 362, 787]
xs 131 231 425 scan op+ xs 131 362 787
scan op*
For example:
scan op* [131,231,425]
would produce:
[131, 30261, 12860925]
xs 131 231 425 scan op* xs 131 30261 12860925
set
scan intersection
For example:
scan intersection [fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([0,1,2]), fromList([231,425])]
would produce:
[fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([2]), empty]
xs {1,2,3,4,5} {2,3,4} {0,1,2} {231,425} scan op+ xs {1,2,3,4,5} {2,3,4} {2} {}
scan union
For example:
scan intersection [fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([0,1,2]), fromList([231,425])]
would produce:
[fromList([1,2,3,4,5]), fromList([2,3,4]), fromList([2]), empty]
xs {1,2,3,4,5} {2,3,4} {0,1,2} {231,425} scan op+ xs {1,2,3,4,5} {1,2,3,4,5} {0,1,2,3,4,5} {0,1,2,3,4,5,231,425}
Test
file: | unit_test_scan.sml | |
source folder: | src/test/sml/warmup_scan |