> 1 <

Автор Сообщение

Яков Замир Кацман (нью)

Members


Статус

15 сообщений

Где: Russia
Род занятий:
Возраст:

#7797   2017-11-06 23:23 GMT+3 часа(ов)      
;; Overview to consisting of test examples 
;; to "strict type definition" mechanics
 
;;;; 1 - example
;; all numbers is integer
(defun all-ints (a)
(let ((len (array-total-size a)))
(dotimes (i len)
(unless (integerp (row-major-aref a i))
(return-from all-ints nil)))
t))
 
;; check dimensional array < 2 and square form matrix (simple 2x2, 3x3 e.t.c)
(defun equidimensional (arr)
(and (<= (array-rank arr) 2)
(apply #'= (array-dimensions arr))))
 
;; define array type with equidimensional conditional
(deftype square-matrix (&optional type size)
`(and (array ,type (,size ,size))
(satisfies equidimensional)))
 
;; integer square matrix
(deftype integer-square-matrix (&optional size)
`(and (square-matrix integer ,size)
(satisfies all-ints)))
 
 
(let ((objects ( list :ABC
( make-array '(2 2) :initial-contents
'((( 1 )( 2 ))(( 4 )( 5 ))) )
( make-array '(3 3) :initial-contents
'((( 1 )( 2 )( 3 ))(( 4 )( 5 )( 6 ))(( 7 )( 8 )( 9 ))) )
( make-array '(5 1) :initial-contents
'(( 1.6 )( 2.1 )( 3.3 )( 4.1 )( 5.4 )) )
( make-array '(2 1) :initial-contents
'(( 1.6 )( 2.3 )) )
( make-array '(2 1) :initial-contents
'(( 3 )( 6 )) )
)))
(dolist (obj objects)
(format t "~S~26T" obj)
(typecase obj
(integer-square-matrix
(format t "is an integer square matrix"))
(square-matrix
(format t "square matrix range less 3"))
(array
(format t "is a non-square matrix"))
(t
(format t "is a non-matrix")))
(format t "~%"))
)
 
;;;; 2 - example
 
;;;; satisfies - example
;;;; integer and odd numbers
(defun odd-number-p (x)
(and (integerp x) (oddp x)))
 
(typep "hello" '(satisfies odd-number-p))
NIL
(typep 10 '(satisfies odd-number-p))
NIL
(typep 11 '(satisfies odd-number-p))
T
 
;; typecase - example
;; (Note that the parts of this example which use TYPE-OF
;; are implementation-dependent)
(defun what-is-it (x)
(format t "~&~S is ~A.~%"
x (typecase x
(float "a float")
(null "a symbol, boolean false, or the empty list")
(list "a list")
(t (format nil "a(n) ~(~A~)" (type-of x))))))
 
;; How I understand 'nil means void return type here
(map 'nil #'what-is-it '(nil (a b) 7.0 7 box))


Цитата

God bless the good people that help me make it's sample! (see links below)

[1] http://jtra.cz/stuff/lisp/sclr/make-array.html
[2] http://www.gigamonkeys.com/book/a-few-format-recipes.html
[3] https://blog.cneufeld.ca/2014/01/the-less-familiar-parts-of-lisp-for-beginners-deftype/
[4] https://www.slac.stanford.edu/comp/unix/gnu-info/cl_3.html#SEC12
[5] http://clhs.lisp.se/Body/f_typep.htm
[6] http://clhs.lisp.se/Body/m_tpcase.htm
[7] https://www.slideshare.net/dataminingtools/type-specifiers-in-lisp
[8] https://groups.google.com/forum/#!topic/comp.lang.lisp/w4bYVI7ieoA
[9] https://stackoverflow.com/questions/29768959/using-satisfies-with-and-in-common-lisp

отредактировал(а) Яков Замир Кацман (нью): 2017-11-06 23:35 GMT+3 часа(ов)
> 1 <


Онлайн :

0 пользователь(ей), 27 гость(ей) :




Реклама на сайте: