Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lisp-if语句用于各种操作_Lisp - Fatal编程技术网

Lisp-if语句用于各种操作

Lisp-if语句用于各种操作,lisp,Lisp,这是我的lisp代码 (DEFUN F (A B) (SETF C (* 4 A)) (SETF D (* 2 (EXPT B 3))) (SETF RES (+ C D)) (IF (AND (TYPEP A 'INTEGER) (TYPEP B 'INTEGER)) (list 'Final 'value '= res)

这是我的lisp代码

 (DEFUN F (A B)
              (SETF C (* 4 A))
              (SETF D (* 2 (EXPT B 3)))
              (SETF RES (+ C D))
              (IF (AND (TYPEP A 'INTEGER) (TYPEP B 'INTEGER))
                  (list 'Final 'value '= res)
                '(YOUR INPUTS ARE NOT NUMBERS)))
例如,
(f59)
运行良好。 但是
(f'w'q)
无法处理以下错误消息:

(错误类型-错误数据W预期类型数字格式控制 ~@格式参数 (W编号) 错误:
W'不是预期的类型
NUMBER'

如果A,B是整数,我想计算4A+2B^3

否则,如果至少有一条不是整数打印错误消息

我尝试使用上面显示的代码。 但是如何使用if语句来处理这个错误呢?

首先,应该使用来定义局部变量

(defun f (a b)
  (let* ((c (* 4 a))           ; You need LET* instead of LET because
         (d (* 2 (expt b 3)))  ; RES depends on the previous variables.
         (res (+ c d)))
    (if (and (typep a 'integer) (typep b 'integer))
        (list 'final 'value '= res)
        '(your inputs are not numbers))))
实际问题是,在检查参数是否为整数之前,您正在进行计算。如果,则应在
中移动计算

(defun f (a b)
  (if (and (integerp a) (integerp b))
      (let* ((c (* 4 a))
             (d (* 2 (expt b 3)))
             (res (+ c d)))
        (list 'final 'value '= res))
      '(your inputs are not numbers)))
返回这样的列表有点奇怪。如果要将它们作为用户的输出,则应打印消息并返回实际结果

(defun f (a b)
  (if (and (integerp a) (integerp b))
      (let ((result (+ (* 4 a)
                       (* 2 (expt b 3)))))
        (format t "Final value = ~a~%" result)
        result)                                      ; Return RESULT or
      (format t "Your inputs are not integers.~%"))) ; NIL from FORMAT.
在大多数情况下,如果参数类型不正确,则应发出错误信号。打印执行计算的函数的输出通常不是一个好主意

(defun f (a b)
  (check-type a integer "an integer")
  (check-type b integer "an integer")
  (+ (* 4 a)
     (* 2 (expt b 3))))

(defun main (a b)
  (handler-case
      (format t "Final value = ~a~%" (f a b))
    ;; CHECK-TYPE signals a TYPE-ERROR if the type is not correct.
    (type-error () (warn "Your inputs are not integers."))))

(main 12 1)
; Final value = 50
;=> NIL
(main 12 'x)
; WARNING: Your inputs are not integers.
;=> NIL
公共Lisp条件系统还允许您使用重新启动来修复错误
CHECK-TYPE
建立名为
STORE-VALUE
的重新启动,您可以调用它为位置提供正确的值。在这种情况下,它可能没有意义,但您可以使用
1
作为默认值

(defun main (a b)
  (handler-bind ((type-error (lambda (e)
                               (store-value 1 e))))
    (format t "Final value = ~a~%" (f a b))))

(main 12 1)
; Final value = 50
;=> NIL
(main 12 'x)
; Final value = 50
;=> NIL
请注意,条件/错误处理程序确实会增加一些开销,因此对于性能关键型函数,您可能不想使用它们,而是在调用函数之前检查参数

(declaim (ftype (function (integer integer) integer) f))
(defun f (a b)
  (declare (optimize (speed 3) (safety 0) (debug 0)))
  (+ (* 4 a)
     (* 2 (expt b 3))))

(defun main (a b)
  (if (and (integerp a)
           (integerp b))
      (format t "Final value = ~a~%" (f a b))
      (warn "Your inputs are not integers.")))
首先,您应该使用来定义局部变量

(defun f (a b)
  (let* ((c (* 4 a))           ; You need LET* instead of LET because
         (d (* 2 (expt b 3)))  ; RES depends on the previous variables.
         (res (+ c d)))
    (if (and (typep a 'integer) (typep b 'integer))
        (list 'final 'value '= res)
        '(your inputs are not numbers))))
实际问题是,在检查参数是否为整数之前,您正在进行计算。如果
,则应在
中移动计算

(defun f (a b)
  (if (and (integerp a) (integerp b))
      (let* ((c (* 4 a))
             (d (* 2 (expt b 3)))
             (res (+ c d)))
        (list 'final 'value '= res))
      '(your inputs are not numbers)))
返回这样的列表有点奇怪。如果要将它们作为用户的输出,则应打印消息并返回实际结果

(defun f (a b)
  (if (and (integerp a) (integerp b))
      (let ((result (+ (* 4 a)
                       (* 2 (expt b 3)))))
        (format t "Final value = ~a~%" result)
        result)                                      ; Return RESULT or
      (format t "Your inputs are not integers.~%"))) ; NIL from FORMAT.
在大多数情况下,如果参数类型不正确,则应发出错误信号。打印执行计算的函数的输出通常不是一个好主意

(defun f (a b)
  (check-type a integer "an integer")
  (check-type b integer "an integer")
  (+ (* 4 a)
     (* 2 (expt b 3))))

(defun main (a b)
  (handler-case
      (format t "Final value = ~a~%" (f a b))
    ;; CHECK-TYPE signals a TYPE-ERROR if the type is not correct.
    (type-error () (warn "Your inputs are not integers."))))

(main 12 1)
; Final value = 50
;=> NIL
(main 12 'x)
; WARNING: Your inputs are not integers.
;=> NIL
公共Lisp条件系统还允许您使用重新启动来修复错误
CHECK-TYPE
建立名为
STORE-VALUE
的重新启动,您可以调用它为位置提供正确的值。在这种情况下,它可能没有意义,但您可以使用
1
作为默认值

(defun main (a b)
  (handler-bind ((type-error (lambda (e)
                               (store-value 1 e))))
    (format t "Final value = ~a~%" (f a b))))

(main 12 1)
; Final value = 50
;=> NIL
(main 12 'x)
; Final value = 50
;=> NIL
请注意,条件/错误处理程序确实会增加一些开销,因此对于性能关键型函数,您可能不想使用它们,而是在调用函数之前检查参数

(declaim (ftype (function (integer integer) integer) f))
(defun f (a b)
  (declare (optimize (speed 3) (safety 0) (debug 0)))
  (+ (* 4 a)
     (* 2 (expt b 3))))

(defun main (a b)
  (if (and (integerp a)
           (integerp b))
      (format t "Final value = ~a~%" (f a b))
      (warn "Your inputs are not integers.")))

从初学者的角度来看,有很多帮助。基于你珍贵的材料,我会更加成长。谢谢。从初学者的角度来看,有很多帮助。基于你珍贵的材料,我会更加成长。非常感谢。