为什么LISP在函数中使用and IF语句打印NIL?

为什么LISP在函数中使用and IF语句打印NIL?,lisp,common-lisp,Lisp,Common Lisp,我刚刚开始学习LISP,我正在尝试创建一个程序,当给它一个数字时,打印出字母等级 (defun number-to-letter (number) (if(or(> number 100)(< number 0)) (print "NUMBER OUT OF RANGE, PLEASE ENTER NUMBER WITHIN RANGE 0-100") ) (if(and(>= number 80)(<= number 100)) (print

我刚刚开始学习LISP,我正在尝试创建一个程序,当给它一个数字时,打印出字母等级

(defun number-to-letter (number)

(if(or(> number 100)(< number 0))
    (print "NUMBER OUT OF RANGE, PLEASE ENTER NUMBER WITHIN RANGE 0-100")
)

(if(and(>= number 80)(<= number 100))
        (print "A")
)

(if(and(>= number 70)(<= number 79))
        (print "B")
)

(if(and(>= number 60)(<= number 69))
        (print "C")
)

(if(and(>= number 50)(<= number 59))
        (print "D")
)

(if(and(>= number 0)(<= number 49))
        (print "F")
)
)
(将数字转换为字母(数字)
(如果(或(>100号)(<0号))
(打印“数字超出范围,请输入0-100范围内的数字”)
)

(如果(和(>=数字80)(=数字70)(=数字60)(=数字50)(=数字0)(中对
NIL
部分进行了说明

一些评论

  • 是可变的,您可以写入
    (中的
    NIL
    部分

    一些评论

    • 是可变的,您可以编写
      (当您有一个打印某些内容的函数并从Lisp侦听器调用它时,您不仅可以看到打印的输出,还可以看到表达式返回的值。它可能类似于:

      LISP> (print "a")
      "a" <-- this is the output
      "a" <-- this is the return value
      LISP>
      
      我们可以添加一个替代形式,然后生成其值,例如
      42

      LISP> (if (> 3 4) (print "three is greater than four") 42)
      42
      
      如果我们将这些
      If
      形式放入函数并调用这些函数,同样的事情也会发生

      函数体中可以有两个或两个以上的形式。其行为是将它们作为“隐式
      progn
      ”进行求值。
      progn
      是一种逐个求值其参数形式,然后返回最后一种形式的值的形式。我们可以直接使用它:

      LISP> (progn
              (if (< 3 4) (print "three is less than four"))
              (if (> 3 4) (print "three is greater than four")))
      "three is less than four"
      NIL
      
      Lisp中的
      values
      函数指定了零个或多个结果值。如果没有给它任何参数,那么它就不会产生任何值。如果我们将其放在函数体或
      progn
      或具有类似
      progn
      的体的其他构造的末尾,那么它就不会产生任何值

      例如,ANSI Common Lisp的CLISP实现有一个侦听器,当计算
      (值)
      时,它只打印一个空行:

      clisp -q
      [1]> 1
      1
      [2]> (list 1 2)
      (1 2)
      [3]> (values)
      
      [4]>
      

      当您有一个打印某些内容的函数,并从Lisp侦听器调用它时,您不仅会看到打印的输出,还会看到表达式返回的值。它可能看起来像这样:

      LISP> (print "a")
      "a" <-- this is the output
      "a" <-- this is the return value
      LISP>
      
      我们可以添加一个替代形式,然后生成其值,例如
      42

      LISP> (if (> 3 4) (print "three is greater than four") 42)
      42
      
      如果我们将这些
      If
      形式放入函数并调用这些函数,同样的事情也会发生

      函数体中可以有两个或两个以上的形式。其行为是将它们作为“隐式
      progn
      ”进行求值。
      progn
      是一种逐个求值其参数形式,然后返回最后一种形式的值的形式。我们可以直接使用它:

      LISP> (progn
              (if (< 3 4) (print "three is less than four"))
              (if (> 3 4) (print "three is greater than four")))
      "three is less than four"
      NIL
      
      Lisp中的
      values
      函数指定了零个或多个结果值。如果没有给它任何参数,那么它就不会产生任何值。如果我们将其放在函数体或
      progn
      或具有类似
      progn
      的体的其他构造的末尾,那么它就不会产生任何值

      例如,ANSI Common Lisp的CLISP实现有一个侦听器,当计算
      (值)
      时,它只打印一个空行:

      clisp -q
      [1]> 1
      1
      [2]> (list 1 2)
      (1 2)
      [3]> (values)
      
      [4]>
      

      根据你的说法,你也应该进行失败的测试。根据你的说法,你也应该进行失败的测试。谢谢,这帮助了很多人!