defstruct en Lisp
defstruct declara un registro y genera gratis su constructor, sus accesores y su copiador.
;; A point structure with typed slots, defaults, and generated helpers.
(defstruct point
(x 0.0 :type single-float)
(y 0.0 :type single-float)
(label "origin"))
(defvar *start* (make-point))
(defvar *target* (make-point :x 3.0 :y 4.0 :label "target"))
(defun point-distance (a b)
(sqrt (+ (expt (- (point-x b) (point-x a)) 2)
(expt (- (point-y b) (point-y a)) 2))))
(defvar *shifted* (copy-point *target*))
(setf (point-x *shifted*) 6.0)
(format t "distance ~,2f, shifted x ~,1f~%"
(point-distance *start* *target*) (point-x *shifted*))
Cómo funciona
defstruct pointda axyyun valor por defecto0.0y un:type single-float.make-pointtoma un argumento por palabra clave por ranura, así que*start*conserva todos los defaults.copy-pointclona*target*, y(setf (point-x *shifted*) 6.0)escribe a través de un accesor.
Palabras clave y builtins usados aquí
defstructdefundefvarexptformatsetfsqrt
El intento, en números
- Líneas
- 19
- Caracteres a escribir
- 541
- Tokens
- 115
- Ritmo de tres estrellas
- 75 tpm
Al ritmo de tres estrellas de 75 tokens por minuto, este intento toma unos 92 segundos.
Paso 1 de 3 en Estructuras y CLOS; paso 19 de 27 en Fundamentos del lenguaje.