common-lisp - 在 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 Lisp 没有中缀算术。所有算术都是通过调用函数来完成的,调用函数是通过编写一个左括号、函数名、参数,然后是一个右括号来完成的。
你写的地方:
(area (radius)
(2 * (22/7) * radius))
你可能打算写:
(area (radius)
(* pi radius radius))
(假设您正在尝试计算半径而不是近似圆周)
推荐阅读
- vb.net - 我的 If 中有一个 DBNull 异常,为什么?
- sql - 如何解决带光标遍历表格的无限循环?
- c# - 如何从另一个表单关闭和重新打开一个表单?
- android - 下载管理器在没有互联网访问的情况下无法工作
- python-3.x - 使用回归而不是分类进行多类分类
- mysql - 返回插入语句MySql的选择查询
- node.js - node.js mongoose 跨项目使用相同模式的多个数据库
- c# - 支持 Intellisense 的 C# 模板引擎
- javascript - 如何使用 Vue.js 将计算属性分配给数据?
- javascript - Vue 添加外部 .js 脚本