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

Homework 9 (Due 22 August 2005)

  1. Write a function, sumEveryOther, to calculate the sum of every other number in a list of numbers.  Solution.
;------------------------------------------------------------------------------
; @description calculate the sum of every other number in a list of numbers
; @param L a list of numbers
; @param justAdded a flag to check whether a node has just been added
; @return the sum of every other number
; @contract sumEveryOther: boolean LON -> number
; @example (sumEveryOtherHelper true empty) = 0
; @example (sumEveryOtherHelper false list-1) = 5
; @example (sumEveryOtherHelper false list-2) = 5
; @example (sumEveryOtherHelper false list-3) = 12
;------------------------------------------------------------------------------
(define (sumEveryOtherHelper justAdded L)
  (cond
    [(empty? L) 0]
    [justAdded (sumEveryOtherHelper false (LON-rest L))]
    [else (+ (LON-first L)
             (sumEveryOtherHelper true (LON-rest L)))]))

;------------------------------------------------------------------------------
; @description calculate the sum of every other number in a list of numbers
; @param L a list of numbers
; @return the sum of every other number
; @contract sumEveryOther: LON -> number
; @example (sumEveryOther empty) = 0
; @example (sumEveryOther list-1) = 5
; @example (sumEveryOther list-2) = 5
; @example (sumEveryOther list-3) = 12
;------------------------------------------------------------------------------
(define (sumEveryOther L)
  (cond
    [(empty? L) 0]
    [else (sumEveryOtherHelper false L)]))
  1. Write a function, firstPosition, to find the position of the first occurrence of a number in a list of numbers.
;------------------------------------------------------------------------------
; @description find the position of the first occurrence of a number in a list
;              of numbers
; @param L a list of numbers
; @param num the number to be looked for
; @param the position so far
; @return 0 if num is not in L, oterwise pos
; @contract findFirstHelper : LON number number -> number
; @example (findFirstHelper empty 5 1) = 0
; @example (findFirstHelper list-1 5 2) = 2
; @example (findFirstHelper list-2 5 7) = 7
; @example (findFirstHelper list-3 5 9) = 11
;------------------------------------------------------------------------------
(define (findFirstHelper L num pos)
  (cond
    [(empty? L) 0]
    [(eq? (LON-first L) num) pos]
    [else (findFirstHelper (LON-rest L) num (+ 1 pos)) ] ) )

;------------------------------------------------------------------------------
; @description find the position of the first occurrence of a number in a list
;              of numbers
; @param L a list of numbers
; @param num the number to be looked for
; @return 0 if num is not in L, oterwise the position of the first occurrence
;         of num in L
; @contract findFirst : LON number -> number
; @example (findFirst empty 5)  = 0
; @example (findFirst list-1 5) = 1
; @example (findFirst list-2 5) = 1
; @example (findFirst list-3 5) = 3
;------------------------------------------------------------------------------
(define (findFirst L num)
  (findFirstHelper L num 1))

  1. Write a function, allPosition, to get the list of positions of a number in a list of numbers.
;------------------------------------------------------------------------------
; @description get the list of positions of a number in a list of numbers
; @param L a list of numbers
; @param num the number to be looked for
; @param the position so far
; @param the list of positions so far
; @return 0 if num is not in L, oterwise pos
; @contract allPositionHelper : LON number number LON -> LON
; @example (allPositionHelper empty 5 1 empty) = empty
; @example (allPositionHelper list-1 5 2 empty) = (make-LON 2 empty)
; @example (allPositionHelper list-2 5 7 empty) = (make-LON 7 empty)
; @example (allPositionHelper list-3 5 9 empty) = (make-LON 11 empty)
; @example (allPositionHelper list-4 5 9 empty) = (make-LON 9 (make-LON 11 (make-LON 12 empty)))
;------------------------------------------------------------------------------
(define (allPositionHelper L num pos positions)
  (cond
    [(empty? L) positions]
    [(eq? (LON-first L) num) (make-LON pos (allPositionHelper (LON-rest L) num (+ 1 pos) positions))]
    [else (allPositionHelper (LON-rest L) num (+ 1 pos) positions) ] ) )

;------------------------------------------------------------------------------
; @description get the list of positions of a number in a list of numbers
; @param L a list of numbers
; @param num the number to be looked for
; @return 0 if num is not in L, oterwise pos
; @contract allPosition : LON number -> LON
; @example (allPosition empty 5) = empty
; @example (allPosition list-1 5) = (make-LON 1 empty)
; @example (allPosition list-2 5) = (make-LON 1 empty)
; @example (allPosition list-3 5) = (make-LON 3 empty)
; @example (allPosition list-4 5) = (make-LON 1 (make-LON 3 (make-LON 4 empty)))
;------------------------------------------------------------------------------
(define (allPosition L num)
  (allPositionHelper L num 1 empty))