gpt4 book ai didi

lisp - 我如何在 lisp 中调用另一个函数;

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:40 26 4
gpt4 key购买 nike

我的程序应该将给定温度从华氏度转换为摄氏度或相反。它接受一个包含数字和字母的列表。字母是温度,字母是我们所在的单位。然后我将适当的函数称为 F-to-C 或 C-to-F。如何使用在我的温度转换函数中首先检查的给定列表调用函数。这是我的代码。

 (defun temperature-conversion (lst)
(cond
((member 'F lst) (F-to-C))
((member 'C lst) (C-to-F))
(t (print "You didn't enter a valid unit for conversion"))
)
)
(defun F-to-C ()
;;(print "hello")
(print (temperature-conversion(lst)))
)
(defun C-to-F ()
(print "goodbye"))
;;(print (temperature-conversion '(900 f)))
(setf data1 '(900 f))

最佳答案

你有无限递归:temperature-conversion 调用 F-to-C 再次调用 temperature-conversion

我会这样做:

(defun c2f (c) (+ 32 (/ (* 9 c) 5)))
(defun f2c (f) (/ (* 5 (- f 32)) 9))
(defun temperature-conversion (spec)
(ecase (second spec)
(C (c2f (first spec)))
(F (f2c (first spec)))))
(temperature-conversion '(32 f))
==> 0
(temperature-conversion '(100 c))
==> 212
(temperature-conversion '(100))

*** - The value of (SECOND SPEC) must be one of C, F
The value is: NIL
The following restarts are available:
ABORT :R1 Abort main loop

关于lisp - 我如何在 lisp 中调用另一个函数;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com