Scheme-DrRacket-BSL不理解我的错误信息及其原因

时间:2015-11-14 09:45:37

标签: scheme racket

; TASK-1

; Length is a Number.
; interp. length of a letter in cm.

; Width is a Number.
; interp. width of a letter in cm.

; Height is a number.
; interp. height of a letter in cm.

; Weight is a Number.
; interp. weight of a letter in g.

; Format is one of:
;  - "Standard"
;  - "Kompakt"
;  - "Gross"
;  - "Maxi"
; interp. the products offered by Deutsche Post

(define (function-1 Format)
  (cond
    [(string=? "Standard" Format) ...]
    [(string=? "Kompakt"  Format) ...]
    [(string=? "Gross"    Format) ...]
    [(string=? "Maxi"     Format) ...]
    [else ...]))

; MaybeFormat is one of:
;  - Format
;  - #false
; interp. the best Format choice, or #false if no Format would work

(define-struct MaybeFormat (Format boolean))
; A MaybeFormat is a structure: (make-MaybeFormat Format boolean)
; interp. either a Format or #false


; MaybeFormat -> String or boolean
; decides whether it's a Format or a boolean (#false)

(define (function-2 MaybeFormat)  
  (cond
    [(MaybeFormat? MaybeFormat) ...]
    [else ...]))


; TASK-2

; Length Width Height Weight -> MaybeFormat
; find the cheapest format to send something
(check-expect (cheapest-format 14     9    0.5      20) "Standard")
(check-expect (cheapest-format 23.5   12.5 0.5      20) "Standard")
(check-expect (cheapest-format 10     7    1        50) "Kompakt")
(check-expect (cheapest-format 23.5   12.5 1        50) "Kompakt")
(check-expect (cheapest-format 14     9    0.5      50) "Kompakt")
(check-expect (cheapest-format 10     7    2       500) "Gross")
(check-expect (cheapest-format 35.3   25   2       500) "Gross")
(check-expect (cheapest-format 10     23.5 1       500) "Gross")
(check-expect (cheapest-format 10     7    5      1000) "Maxi")
(check-expect (cheapest-format 35.3   25   5      1000) "Maxi")
(check-expect (cheapest-format 10     7    2      1000) "Maxi")
(check-expect (cheapest-format 10     7    5        10) "Maxi")
(check-expect (cheapest-format 35.3   7    1        50) "Gross")
(check-expect (cheapest-format 14     25   0.5      20) "Gross")
(check-expect (cheapest-format 50     9    0.5      20) #false)
(check-expect (cheapest-format 10     50   1        50) #false)
(check-expect (cheapest-format 10     9    500      20) #false)
(check-expect (cheapest-format 10     9    1      9001) #false)
(check-expect (cheapest-format 0      0    0         0) #false)
(check-expect (cheapest-format 0      9    2        20) #false)
(check-expect (cheapest-format 10     0    0.5      50) #false)
(check-expect (cheapest-format 10     9    0        20) #false)
(check-expect (cheapest-format 10     9    0.5       0) #false)

(define (cheapest-format length width height weight)
  (cond
    [(format-possible? "Standard" length width height weight)
     "Standard"]
    [(format-possible? "Kompakt" length width height weight)
     "Kompakt"]
    [(format-possible? "Gross" length width height weight)
     "Gross"]
    [(format-possible? "Maxi" length width height weight)
     "Maxi"]
    [else
     #false]))


;TASK-3


; Format Length Width Height Weight -> Boolean
; check whether we can send something as a given format
(define (format-possible? format length width height weight)
  (and (in-range? length (min-length format) (max-length format))
       (in-range? width (min-width format) (max-width format))
       (below-maximum? height (max-height format))
       (below-maximum? weight (max-weight format))
       (above-minimum? (aspect-ratio length width)
                       (min-aspect-ratio format))))


; Maximum is a Number.
; interp. the upper bound of something

; Minimum is a Number.
; interp. the lower bound of something


; in-range?
; Number Minimum Maximum -> Boolean
; Check whether a number is between a minimum and a maximum
(check-expect (in-range? 5   0   10) #true)
(check-expect (in-range? 50  8   42) #false)
(define (in-range? number Minimum Maximum)
  (cond
    [(>= number Minimum) #true]
    [(<= number Maximum) #true]
    [else #false]))

; above-minimum?
; Number MaybeMinimum -> Boolean
; Check whether a number is above an optional minimum
(check-expect (above-minimum? 42 13) #true)
(check-expect (above-minimum? 35 99) #false)
(define (above-minimum? number sMaybeMinimum)
  (cond
    [(>  number sMaybeMinimum) #true]
    [(<= number sMaybeMinimum) #false]))

; below-maximum?
; Number Maximum -> Boolean
; Check whether a number is below a maximum
(check-expect (below-maximum? 10 15) #true)
(check-expect (below-maximum? 42 10) #false)
(define (below-maximum? number Maximum)
  (cond
    [(<  number Maximum)#true]
    [(>= number Maximum)#false]))

; min-length
; Format -> Minimum
; return minimum length of a format
(check-expect (min-length "Standard") 14)
(check-expect (min-length "Maxi")     10)
(check-expect (min-length "Kompakt")  10)
(check-expect (min-length "Gross")    10)
(define (min-length Format)
 (cond
   [(string=? "Standard" Format) 14]
   [(string=? "Kompakt"  Format) 10]
   [(string=? "Gross"    Format) 10]
   [(string=? "Maxi"     Format) 10]))

; max-length
; Format -> Maximum
; return maximum length of a format
(check-expect (max-length "Standard") 23.5)
(check-expect (max-length "Maxi")     35.3)
(check-expect (max-length "Kompakt")  23.5)
(check-expect (max-length "Gross")    35.3)
(define (max-length Format)
   (cond
   [(string=? "Standard" Format) 23.5]
   [(string=? "Kompakt"  Format) 23.5]
   [(string=? "Gross"    Format) 35.3]
   [(string=? "Maxi"     Format) 35.3]))

; min-width
; Format -> Minimum
; return minimum width of a format
(check-expect (min-width "Standard")  9)
(check-expect (min-width "Maxi")      7)
(check-expect (min-width "Kompakt")   7)
(check-expect (min-width "Gross")     7)
(define (min-width Format)
    (cond
   [(string=? "Standard" Format) 9]
   [(string=? "Kompakt"  Format) 7]
   [(string=? "Gross"    Format) 7]
   [(string=? "Maxi"     Format) 7]))



; max-width
; Format -> Maximum
; return maximum width of a format
(check-expect (min-width "Standard") 12.5)
(check-expect (min-width "Maxi")       25)
(check-expect (min-width "Kompakt")  12.5)
(check-expect (min-width "Gross")      25)
(define (max-width Format)
    (cond
   [(string=? "Standard" Format) 12.5]
   [(string=? "Kompakt"  Format) 12.5]
   [(string=? "Gross"    Format)   25]
   [(string=? "Maxi"     Format) 25]))

; max-height
; Format -> Maximum
; return maximum height of a format
(check-expect (max-height "Standard")  0.5)
(check-expect (max-height "Maxi")        5)
(check-expect (max-height "Kompakt")     1)
(check-expect (max-height "Gross")       2)
(define (max-height Format)
    (cond
   [(string=? "Standard" Format)  0.5]
   [(string=? "Kompakt"  Format)    1]
   [(string=? "Gross"    Format)    2]
   [(string=? "Maxi"     Format)    5]))

; max-weight
; Format -> Maximum
; return maximum weight of a format
(check-expect (max-weight "Standard")   20)
(check-expect (max-weight "Maxi")     1000)
(check-expect (max-weight "Kompakt")    50)
(check-expect (max-weight "Gross")     500)
(define (max-weight Format)
    (cond
   [(string=? "Standard" Format)   20]
   [(string=? "Kompakt"  Format)   50]
   [(string=? "Gross"    Format)  500]
   [(string=? "Maxi"     Format) 1000]))


; AspectRatio is a Number.
; interp. ratio of length to width

; aspect-ratio
; Length Width -> AspectRatio
; compute aspect ratio of letter
(check-expect (aspect-ratio 50   10)      5)
(check-expect (aspect-ratio 23.5 12.5) 1.88)
(check-expect (aspect-ratio 35 7)         5)
(define (aspect-ratio Length Width)
  (/ Length Width))

; min-aspect-ratio
; Format -> MaybeMinimum
; return optional minimum aspect ratio of format
(check-expect (min-aspect-ratio "Standard")   1.4)
(check-expect (min-aspect-ratio "Kompakt")    1.4)
(check-expect (min-aspect-ratio "Gross")   #false)
(check-expect (min-aspect-ratio "Maxi")    #false)
(define (min-aspect-ratio Format)
      (cond
   [(string=? "Standard" Format)   1.4]
   [(string=? "Kompakt"  Format)   1.4]
   [(string=? "Gross"    Format)#false]
   [(string=? "Maxi"     Format)#false]))



; MaybeMinimum is one of:
;  - #false
;  - Minimum`
; interp. the lower bound of something, or #false if there's no lower bound

(define (maybe-minimum-template maybe-minimum)
  (if (false? maybe-minimum)
     ...
     (... maybe-minimum ...)))

当我运行此代码时,它给出了以下错误: “&gt;:预期真实的第二个参数,给定#false”

我不明白为什么。当我隔离应该产生错误的函数时,它可以完美地工作。你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

min-aspect-ratio返回&#34; Gross&#34;的布尔值和&#34; Maxi&#34;。因此,在format-possible?中,测试(above-minimum? (aspect-ratio length width) (min-aspect-ratio format))失败。

我不熟悉BSL,但这些改变应该有所帮助:

(define (aspect-ratio Length Width)
  (if (zero? Width)                          ; <=====
      #false                                 ; <=====
      (/ Length Width)))                      

(define (above-minimum? number sMaybeMinimum)
  (cond
    [(boolean? number) number]               ; <=====
    [(boolean? sMaybeMinimum) sMaybeMinimum] ; <=====
    [(>  number sMaybeMinimum) #true]
    [(<= number sMaybeMinimum) #false]))

没有更多错误,但60次测试中有20次失败。