Difference between revisions of "Scan Higher Order Function Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 10: Line 10:
  
 
  fun scan operation xs
 
  fun scan operation xs
 +
 +
===signature===
 +
val scan = fn : ('a * 'a -> 'a) -> 'a list -> 'a list
  
 
=Clients=
 
=Clients=

Revision as of 09:10, 16 February 2022

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 Smlnj-logo.png
functions: scan

scan

Note: this higher-order function is curried.

fun scan operation xs

signature

val scan = fn : ('a * 'a -> 'a) -> 'a list -> 'a list

Clients

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

Test

file: unit_test_scan.sml
source folder: src/test/sml/warmup_scan