gpt4 book ai didi

inheritance - 普通 lisp 中的多个构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:50 25 4
gpt4 key购买 nike

在 common-lisp 中,类可以有多个构造函数和/或复制构造函数吗?也就是说 - 为了为新向量创建一个类 - “vecr” 来表示实数的 3 维向量,我想定义可以通过多种方式初始化的新类:

(vecr 1.2) ==> #(1.2 1.2 1.2)

(vecr 1.2 1.4 3.2) ==> #(1.2 4.3 2.5)

(vecr) ==> #(0.0 0.0 0.0)

最佳答案

我不知道如何评论上面所说的内容:

This function works well to create a default #(0.0 0.0 0.0) type of vector. However, (vecr 1.0) ==> #(1.0 0.0 0.0) instead of the intended #(1.0 1.0 1.0). I suppose the way around this is to check whether all three were passed, or just one of the optional arguments. – Shamster 6 hours ago

你可以这样做:

(defun vecr (&optional (x 0.0) (y x) (z y))
(vector x y z))

关于inheritance - 普通 lisp 中的多个构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1659413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com