Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EusLispでの有理数クラスのadd-rat関数の呼び出し方がわかりません #399

Open
takky413 opened this issue Oct 18, 2021 · 1 comment

Comments

@takky413
Copy link

takky413 commented Oct 18, 2021

課題2について質問です。

ratクラスを

(defclass rat
	:super propertied-object
	:slots (x))
(defmethod rat
	(:init
		(&optional n d)
		(send self :make-rat n d)
		self)
	(:make-rat (n d) (setq x (cons n d)))
	(:numer () (car x))
	(:denom () (cdr x))
	(:add-rat (x y)
		(:make-rat (+ (* (:numer x) (:denom y))
			(* (:numer y) (:denom x)))
		(* (:denom x) (:denom y))))

		(setq z (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x)) ) (* (cdr x) (cdr y)) )))
	(:print-info
		()
		(send-all members :print-info)))

のように定義したのですが、

8.irteusgl$ setq a (instance rat :init)
#<rat #X55c19d49e418>
9.irteusgl$ send a :make-rat 1 2
(1 . 2)
10.irteusgl$ setq b (instance rat :init)
#<rat #X55c19d506c40>
11.irteusgl$ send b :make-rat 1 3
(1 . 3)
12.irteusgl$ setq c (instance rat :init)
#<rat #X55c19d509010>
13.irteusgl$ send c :add-rat a b
Call Stack (max depth: 20):
  0: at (send c :add-rat a b)
  1: at #<compiled-code #X55c19d1d43e8>
irteusgl 0 error: undefined function :make-rat in (send c :add-rat a b)

としてadd-ratを呼び出そうとすると、関数が定義されていないというエラーが出てしまいました。

また、変数cにadd-rat関数の返り値を代入しようと思って、
「setq c :add-rat a b」
とすると、cにadd-rat関数そのものが代入されてしまいうまく行きませんでした。

:add-rat関数はどのようにして呼び出せば良いのでしょうか。

@k-okada
Copy link
Member

k-okada commented Oct 18, 2021

  (:add-rat (x y)
            (send self :make-rat (+ (* (:numer x) (:denom y))

みたいに (send self ... として自分自身のメンバ関数を呼び出します.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants