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

Homework 3 (Due Monday 27 June 2548)

1. Write a function to calculate the cost of shipping according to the following table. Solution.

First 500kg

15 ฿ / kg
Next 1,000kg

14 ฿ / kg

Next 5,00kg

13 ฿ / kg

Next 10,000kg

12 ฿ / kg

Else

10 ฿ / kg



;----------------------------------------------------------------------------
; @description calculate the cost of shipping according to the following table
;              First   500 kg       15 Baht/kg
;              Next  1,000 kg       14 Baht/kg
;              Next  5,000 kg       13 Baht/kg
;              Next 10,000 kg       12 Baht/kg
;              Anything above       10 Baht/kg
; @param weight the weight of the shipment
; @return the cost of shipping
; @contract shippingCost : number -> number
; @example (shippingCost 400) = 6000
; @example (shippingCost 500) = 7500
; @example (shippingCost 1500) = 21500
; @example (shippingCost 3000) = 41000
; @example (shippingCost 6500) = 86500
; @example (shippingCost 10000) = 128500
; @example (shippingCost 16500) = 206500
; @example (shippingCost 30000) = 341500
;----------------------------------------------------------------------------
(define (shippingCost weight)
  (cond
    [(<= weight 500) (* 15 weight)]
    [(<= weight 1500) (+ (* 14 (- weight 500) ) 7500)]
    [(<= weight 6500) (+ (* 13 (- weight 1500) ) 21500)]
    [(<= weight 16500) (+ (* 12 (- weight 6500) ) 86500)]
    [else (+ (* 10 (- weight 16500) ) 206500)]))

;------
; Test
;------
"(shippingCost 400) = " (shippingCost 400)
"(shippingCost 500) = " (shippingCost 500)
"(shippingCost 1500) = " (shippingCost 1500)
"(shippingCost 3000) = " (shippingCost 3000)
"(shippingCost 6500) = " (shippingCost 6500)
"(shippingCost 10000) = " (shippingCost 10000)
"(shippingCost 16500) = " (shippingCost 16500)
"(shippingCost 30000) = " (shippingCost 30000)