Lisp等级到字母的转换

Ham*_*mad 1 lisp common-lisp

问题问题:

Define a LISP function SCORE->GRADE which takes a single argument, s, and returns a symbol according to the following scheme:
 s ? 90 A           73 ? s < 77 C+
 87 ? s < 90 A–     70 ? s < 73 C
 83 ? s < 87 B+     60 ? s < 70 D
 80 ? s < 83 B      s < 60 F
 77 ? s < 80 B–
 If the argument s is not a number then the function should return NIL.
Run Code Online (Sandbox Code Playgroud)

我的答案是这样的:

 (defun SCORE->GRADE (s)
        (if (not (numberp s))  (return-from SCORE->GRADE “NIL”))
        (progn 
        (if (>= s 90) (return-from SCORE->GRADE "A"))
        (if (and (>= s 87) (< s 90)) (format nil “A-“))
        (if (and (>= s 83) (< s 87)) (format nil “B+”))
        (if (and (>= s 80) (< s 83)) (return-from SCORE->GRADE “B”))
        (if (and (>= s 77) (< s 80)) (return-from SCORE->GRADE “B-“))
        (if (and (>= s 73) (< s 77)) (return-from SCORE->GRADE “C+”))
        (if (and (>= s 70) (< s 73)) (return-from SCORE->GRADE “C”))
        (if (and (>= s 60) (< s 70)) (return-from SCORE->GRADE “D”)
        (if (< s 60) (return-from SCORE->GRADE “F”)) 
        )
      )
    )
Run Code Online (Sandbox Code Playgroud)

它适用于90,返回A,然后对于其他任何东西它只是给出了这个错误,关于我输入的内容有不同的变量

*** - RETURN-FROM:变量"B"没有值

*** - IF:变量"A-"没有值

任何人都可以解释为什么我不能得到相同结果的每一行非常相同?

我已经尝试过消息,格式化t,案例,有些工作可以达到前3个案例然后停止.一直无法解决任何问题.

cor*_*ump 5

除了其他答案之外,请注意,在您的情况下,您不需要复制公共边界,因为您尝试将分数分成等级.

(cond
  ((>= s 90) "A")
  ((>= s 87) "A-")
  ((>= s 83) "B+")
  ...
  ((>= s 70) "C")
  ((>= s 60) "D")
  (t "F"))
Run Code Online (Sandbox Code Playgroud)

它还减少了必须保留在代码中的不变量的数量,这有助于维护.