My very first application in Lisp (LispWorks) is a calculator which works
quite well.
I'm not satisfied with this code
(cond
((string= *op* "+") (+ *y* *x*))
((string= *op* "*") (* *y* *x*))
((string= *op* "/") (/ *y* *x*))
((string= *op* "-") (- *y* *x*))
((string= *op* nil) *x*)))
Could be one line
(funcall #'(??? *op*) *x* *y*)
isn't it? But I'm missing the conversion from string to symbol!
Thx Jens
"Jens Teich" <·················@t-online.de> writes:
> My very first application in Lisp (LispWorks) is a calculator which
> works quite well.
>
> I'm not satisfied with this code
>
> (cond
> ((string= *op* "+") (+ *y* *x*))
> ((string= *op* "*") (* *y* *x*))
> ((string= *op* "/") (/ *y* *x*))
> ((string= *op* "-") (- *y* *x*))
> ((string= *op* nil) *x*)))
>
> Could be one line
>
> (funcall #'(??? *op*) *x* *y*)
>
> isn't it? But I'm missing the conversion from string to symbol!
(intern *op* :cl)
However keep in mind that FUNCTION (a.k.a. #') doesn't evaluate the
name it is given so you can't say #'(intern *op* :cl). However FUNCALL
takes a "function designator" which includes a symbol that names a
globally defined function so you can say:
(funcall (intern *op* :cl) *x* *y*)
-Peter
--
Peter Seibel ·····@javamonkey.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
Works great, thanks.
By the way: I'm learning with your book (recommended by Edi Weitz). It is
very helpful. Thanks for that too. I will send you some comments soon!
Jens
Peter Seibel wrote:
> "Jens Teich" <·················@t-online.de> writes:
>
>> My very first application in Lisp (LispWorks) is a calculator which
>> works quite well.
>>
>> I'm not satisfied with this code
>>
>> (cond
>> ((string= *op* "+") (+ *y* *x*))
>> ((string= *op* "*") (* *y* *x*))
>> ((string= *op* "/") (/ *y* *x*))
>> ((string= *op* "-") (- *y* *x*))
>> ((string= *op* nil) *x*)))
>>
>> Could be one line
>>
>> (funcall #'(??? *op*) *x* *y*)
>>
>> isn't it? But I'm missing the conversion from string to symbol!
>
> (intern *op* :cl)
>
> However keep in mind that FUNCTION (a.k.a. #') doesn't evaluate the
> name it is given so you can't say #'(intern *op* :cl). However FUNCALL
> takes a "function designator" which includes a symbol that names a
> globally defined function so you can say:
>
> (funcall (intern *op* :cl) *x* *y*)
>
> -Peter
"Jens Teich" <·················@t-online.de> writes:
> Works great, thanks.
>
> By the way: I'm learning with your book (recommended by Edi Weitz). It is
> very helpful. Thanks for that too. I will send you some comments soon!
Better hurry! Several chapters are already through copy-edit and on
their way to production. ;-) (Of course there are still two chapters
remaining to be written.)
-Peter
--
Peter Seibel ·····@javamonkey.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Matthew Danish
Subject: Re: Newbie: Convert String to Symbol?
Date:
Message-ID: <87ekhaowqk.fsf@mapcar.org>
Peter Seibel <·····@javamonkey.com> writes:
> (funcall (intern *op* :cl) *x* *y*)
I don't like the use of INTERN here. The intent is not to create a
symbol but to use an already existing one. INTERN will do that; but
it is intended for creating new symbols too. FIND-SYMBOL, as Wade
also proposed, seems more appropriate, because you don't have to worry
about possibly creating new symbols depending on user-input.
Alternatively, you can sanity-check the input.
--
;; Matthew Danish -- user: mrd domain: cmu.edu
;; OpenPGP public key: C24B6010 on keyring.debian.org
Matthew Danish <··········@cmu.edu> writes:
> Peter Seibel <·····@javamonkey.com> writes:
>> (funcall (intern *op* :cl) *x* *y*)
>
> I don't like the use of INTERN here. The intent is not to create a
> symbol but to use an already existing one. INTERN will do that; but
> it is intended for creating new symbols too. FIND-SYMBOL, as Wade
> also proposed, seems more appropriate, because you don't have to worry
> about possibly creating new symbols depending on user-input.
> Alternatively, you can sanity-check the input.
Good point.
-Peter
--
Peter Seibel ·····@javamonkey.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
Jens Teich wrote:
> My very first application in Lisp (LispWorks) is a calculator which
> works quite well.
>
> I'm not satisfied with this code
>
> (cond
> ((string= op "+") (+ y x))
> ((string= op "*") (* y x))
> ((string= op "/") (/ y x))
> ((string= op "-") (- y x))
> ((string= op nil) x)))
>
> Could be one line
>
> (funcall #'(??? op) x y)
>
> isn't it? But I'm missing the conversion from string to symbol!
One "trick" I like to do, is to create a has table of strings or
symbols that can be used for evaluation purposes later (especially when
the symbols are specific and well defined). For example:
(defparameter *ops* (make-hash-table :test #'equalp))
(defmacro defop (symbol function)
`(setf (get-hash ,(symbol-name symbol) *ops*) ,function))
(defop + #'+)
(defop - #'-)
; ...
(defun eval-op (op x y)
(let ((function (get-hash op *ops*)))
(when function
(funcall function x y))))
This can be done numerous other ways as well (eg, using a list an
ASSOC). Keep in mind, I wouldn't do this for your particular problem,
simply because your symbols/functions map 1-to-1 and have the same
name. However, this "trick" is very nice when mapping assembler
mnemonics, etc. HTH,
Jeff M.
--
http://www.retrobyte.org
··············@gmail.com