首页 > 解决方案 > 在 clisp 中定义对象

问题描述

我正在尝试做一个包含 CLISP 的项目。我对 CLISP 没有任何了解,并且完全是这门语言的新手。

以下是已经给出的代码:

    #|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; POLYMORPHISM

TODO 2a. Define an object "cirle" with variables x,y
    (for  the center of the circle) and radius 
    (to hold the size of the circle). Add a method 
    "area" that returns 2 *pi*radius^2

; run this to peek inside circle
'(xpand (circle))

TODO 2b. Define an object "rectangle" with variables x1,x2,y1,y2
    that all default value of 0. Add
    a method "area" that returns the area of that rectangle
TODO 2c. Show the output from the following test

|#

(defun polymorphism()
  (let ((sum 0)
        (all (list (circle :radius 1) 
                   (rectangle :x2 10 :y2 10)
                   (circle :radius 2))))
    (dolist (one all)
      (incf sum (send one 'area)))
    (print `(polymorphism ,sum))))

; to run, uncomment the following
'(polymorphism)

#|

我必须为具有属性和方法的圆形和矩形创建一个对象。

对于圈子,这是我已经尝试过的:

(defthing
  circle
  :has  ((x 0) (y 0) (radius 0))
  :does ((area (radius)
                   (2 * (22/7) * radius))
         ))

对于矩形,这是我已经尝试过的:

(defthing
  rectangle
  :has  ((x1 0) (y1 0) (x2 0) (y2 0))
  :does ((area
                   ((x1-x2) * (y1-y2) * radius))
         ))

这就是我所需要的,还是我必须添加任何东西才能使圆形和矩形方法起作用?

标签: common-lispclisp

解决方案


Common Lisp 没有中缀算术。所有算术都是通过调用函数来完成的,调用函数是通过编写一个左括号、函数名、参数,然后是一个右括号来完成的。

你写的地方:

                (area (radius)
                   (2 * (22/7) * radius))

你可能打算写:

                (area (radius)
                   (* pi radius radius))

(假设您正在尝试计算半径而不是近似圆周)


推荐阅读