Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

How do I call another function in lisp;

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

I would do this:

(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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...