Difference between revisions of "Sum Scan Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
(Created page with "=Background= [https://en.wikipedia.org/wiki/Prefix_sum Prefix sum on Wikipedia] =Code to Implement= {{SMLToImplement|sum_scan|sum_scan|warmup_sum_scan}} ==sum_scan== Write...")
 
Line 22: Line 22:
 
  [1,3,6,10,15,21,28,36]
 
  [1,3,6,10,15,21,28,36]
  
:{| class="wikitable"
+
:{| class="wikitable" style="text-align:right;"
 
|-
 
|-
!xs
+
! style="text-align:right;" | xs  
 
| 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8  
 
| 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8  
 
|-
 
|-
!sum_scan(xs)
+
! style="text-align:right;" | sum_scan(xs)  
 
| 1 || 3 || 6 || 10 || 15 || 21 || 28 || 36  
 
| 1 || 3 || 6 || 10 || 15 || 21 || 28 || 36  
 
|}
 
|}

Revision as of 04:52, 21 January 2022

Background

Prefix sum on Wikipedia

Code to Implement

file: src/main/sml/warmup_sum_scan/sum_scan.sml Smlnj-logo.png
functions: sum_scan

sum_scan

Write a function

fun sum_scan(xs : int list) : int list

which produces a list whose values are each the sum of values in xs up to and including the value at its location.

Example [1,2,3,4,5,6,7,8]

For example:

sum_scan([1,2,3,4,5,6,7,8])

would produce:

[1,3,6,10,15,21,28,36]
xs 1 2 3 4 5 6 7 8
sum_scan(xs) 1 3 6 10 15 21 28 36

Example [131,231,425]

For example:

sum_scan([131,231,425])

would produce:

[131, 362, 787]
xs 131 231 425
sum_scan(xs) 131 362 787


Test

file: unit_test_sum_scan.sml
source folder: src/test/sml/warmup_sum_scan