Copyright © 2005 Suradet Jitprapaikulsarn
สงวนลิขสิทธิ์ 2548 สุรเดช จิตประไพกุลศาล

Homework 10 (Due 29 August 2005)

  1. Write a function, findAt, to find a value a specific position in a list of numbers using accumulator.  Solution.
;------------------------------------------------------------------------------
; @description find the value at a specific position in a list of positions
; @param L a list of numbers
; @param pos a specific position
; @param output the return value
; @return 'NotFound if there is no value a specific position, otherwise
;         the value at a specific position
; @contract findAtHelper : LON number symbol -> number
; @example (findAtHelper empty 2 'NotFound) = 'NotFound
; @example (findAtHelper list-1 1 'NotFound) = 5
; @example (findAtHelper list-2 4 'NotFound) = 'NotFound
; @example (findAtHelper list-3 2 'NotFound) = 9
; @example (findAtHelper list-4 2 'NotFound) = 7
;------------------------------------------------------------------------------
(define (findAtHelper L pos output)
  (cond
    [(empty? L) output]
    [(eq? pos 1) (LON-first L)]
    [else (findAtHelper (LON-rest L) (- pos 1) output)] ) )

;------------------------------------------------------------------------------
; @description find the value at a specific position in a list of positions
; @param L a list of numbers
; @param pos a specific position
; @return 'NotFound if there is no value a specific position, otherwise
;         the value at a specific position
; @contract findAt : LON number -> number
; @example (findAt empty 2) = 'NotFound
; @example (findAt list-1 1) = 5
; @example (findAt list-2 4) = 'NotFound
; @example (findAt list-3 2) = 9
; @example (findAt list-4 2) = 7
;------------------------------------------------------------------------------
(define (findAt L pos)
  (findAtHelper L pos 'NotFound))

  1. Write a function, sumN, to calculate the sum of first n numbers in a list of numbers using accumulator.
;------------------------------------------------------------------------------
; @description calculate the sum of the first N numbers in a list of numbers
; @param L a list of numbers
; @param N the number of numbers to be summed
; @param acc the accumulated sum so far
; @return the sum of the first N numbers of L if L has at least N numbers
;         otherwise, the sum of elements in L
; @contract sumNHelper : LON number number -> number
; @example (sumNHelper empty 2 6) = 6
; @example (sumNHelper list-1 1 2) = 7
; @example (sumNHelper list-2 4 4) = 11
; @example (sumNHelper list-3 2 0) = 16
; @example (sumNHelper list-4 4 0) = 22
;------------------------------------------------------------------------------
(define (sumNHelper L N acc)
  (cond
    [(empty? L) acc]
    [(eq? N 0) acc]
    [else (sumNHelper (LON-rest L) (- N 1) (+ (LON-first L) acc)) ] ) )

;------------------------------------------------------------------------------
; @description find the value at a specific position in a list of positions
; @param L a list of numbers
; @param N the number of numbers to be summed
; @return the sum of the first N numbers of L if L has at least N numbers
;         otherwise, the sum of elements in L
; @contract sumN : LON number -> number
; @example (sumN empty 2) = 0
; @example (sumN list-1 1) = 5
; @example (sumN list-2 4) = 7
; @example (sumN list-3 2) = 16
; @example (sumN list-4 4) = 22
;------------------------------------------------------------------------------
(define (sumN L N)
  (sumNHelper L N 0))