我如何在Lisp中调用另一个函数; [英] How do I call another function in lisp;

查看:1023
本文介绍了我如何在Lisp中调用另一个函数;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My program is supposed to convert a given temperature from Fahrenheit to Centigrade or the other way around. It takes in a list containing a number and a letter. The letter is the temperature and the letter is the unit we are in. Then I call the appropriate function either F-to-C or C-to-F. How do I call the functions with the given list that was first checked in my temperature-conversion function. Here is my code.

 (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.

You have infinite recursion: temperature-conversion calls F-to-C which calls temperature-conversion again.

我会这样做:

(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中调用另一个函数;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆