Guest User

Untitled

a guest
Jul 6th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 5.22 KB | None | 0 0
  1. (defvar *album*)
  2.  
  3. (defun quotient (x y)
  4.   (/ (- x (mod x y)) y))
  5.  
  6. (defclass time-spec ()
  7.   ((min :initform 0)
  8.    (sec :initform 0)))
  9.  
  10. (defmethod t-min ((time-spec time-spec))
  11.   (slot-value time-spec 'min))
  12.  
  13. (defmethod t-sec ((time-spec time-spec))
  14.   (slot-value time-spec 'sec))
  15.  
  16. (defmethod set-min ((time-spec time-spec) min)
  17.   (if (and (typep min 'integer)
  18.            (>= min 0))
  19.       (setf (slot-value time-spec 'min) min)        
  20.     (error "Value must be an integer, greater than 0"))
  21.   time-spec)
  22.  
  23. (defmethod set-sec ((time-spec time-spec) sec)
  24.   (if (and (typep sec 'integer)
  25.            (>= sec 0))
  26.       (setf (slot-value time-spec 'sec) sec)      
  27.     (error "Value must be an integer, greater than 0"))
  28.   time-spec)
  29.  
  30. (defmethod time-in-seconds ((time-spec time-spec))
  31.   (+ (* (t-min time-spec) 60) (t-sec time-spec)))
  32.  
  33. (defmethod set-time-in-seconds ((time-spec time-spec) time-in-sec)
  34.   (let* ((sec (mod time-in-sec 60))
  35.          (min (quotient time-in-sec 60)))
  36.     (set-min time-spec min)
  37.     (set-sec time-spec sec)
  38.     time-spec))  
  39.  
  40. (defun make-time (min sec)
  41.   (let ((time-instance (make-instance 'time-spec)))
  42.     (set-min time-instance min)
  43.     (set-sec time-instance sec)
  44.     time-instance))
  45.  
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47.  
  48. (defclass track ()
  49.   ((name :initform nil)
  50.    (len :initform (make-time 0 0))))
  51.  
  52. (defmethod name ((track track))
  53.   (slot-value track 'name))
  54.  
  55. (defmethod len ((track track))
  56.   (slot-value track 'len))
  57.  
  58. (defmethod set-name ((track track) name)
  59.   (if (typep name 'string)
  60.       (setf (slot-value track 'name) name)
  61.     (error "Name of the track must be a string"))
  62.   track)
  63.  
  64. (defmethod set-len ((track track) time-instance)
  65.   (setf (slot-value track 'len) time-instance)
  66.   track)
  67.  
  68. (defmethod print-track ((track track))
  69.   (format t "~A~1,40T~2D:~2D~%" (name track) (t-min (len track)) (t-sec (len track)))
  70.   track)
  71.  
  72. (defun make-track (name time)
  73.   (let ((track-instance (make-instance 'track)))
  74.     (set-name track-instance name)
  75.     (set-len track-instance time)
  76.     track-instance))
  77.  
  78. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  79.            
  80. (defclass album ()
  81.   ((artist :initform nil)
  82.    (title :initform nil)
  83.    (tracks :initform nil)
  84.    (year-of-release :initform 0)))
  85.  
  86. (defmethod artist ((album album))
  87.   (slot-value album 'artist))
  88.  
  89. (defmethod title ((album album))
  90.   (slot-value album 'title))
  91.  
  92. (defmethod tracks ((album album))
  93.   (slot-value album 'tracks))
  94.  
  95. (defmethod year-of-release ((album album))
  96.   (slot-value album 'year-of-release))
  97.  
  98. (defmethod set-artist ((album album) artist)
  99.   (if (typep artist 'string)
  100.       (setf (slot-value album 'artist) artist)
  101.     (error "Name of the artist must be a string"))
  102.   album)
  103.  
  104. (defmethod set-title ((album album) title)
  105.   (if (typep title 'string)
  106.       (setf (slot-value album 'title) title)
  107.     (error "Name of the album must be a string"))
  108.   album)
  109.  
  110. (defmethod set-tracks ((album album) tracks)  
  111.   (setf (slot-value album 'tracks) tracks)
  112.   album)    
  113.  
  114. (defmethod set-year-of-release ((album album) year)
  115.   (if (typep year 'integer)
  116.       (setf (slot-value album 'year-of-release) year)
  117.     (error "Year must be an integer"))
  118.   album)
  119.  
  120. (defmethod track-count ((album album))
  121.   (length (slot-value album 'tracks)))
  122.                        
  123. (defmethod album-length ((album album))
  124.   (let* ((temp-sec (apply #'+ (mapcar (lambda (x) (t-sec (len x))) (slot-value album 'tracks))))
  125.          (sec (mod temp-sec 60))
  126.          (min (+ (apply #'+ (mapcar (lambda (x) (t-min (len x))) (slot-value album 'tracks)))
  127.                  (quotient temp-sec 60))))
  128.     (make-time min sec)))
  129.  
  130. (defun make-album (artist title tracks year-of-release)
  131.   (let ((album (make-instance 'album)))
  132.     (set-artist album artist)
  133.     (set-title album title)
  134.     (set-tracks album tracks)
  135.     (set-year-of-release album year-of-release)
  136.     album))
  137.  
  138. (defmethod print-headlines ((album album))
  139.   (format t "Album ~D by ~D, released ~D~%~%"
  140.           (title album)
  141.           (artist album)
  142.           (year-of-release album)))
  143.  
  144. (defmethod print-tracks ((album album))
  145.   (dotimes (x (track-count album))
  146.     (format t "~D. " (+ x 1))
  147.     (print-track (nth x (tracks album)))))
  148.  
  149. (defmethod print-total-length ((album album))
  150.   (format t "Total length: ~D:~D"
  151.           (t-min (album-length album))
  152.           (t-sec (album-length album))))
  153.  
  154. (defmethod print-album ((album album))
  155.   (print-headlines album)
  156.   (print-tracks album)
  157.   (print-total-length album))
  158.  
  159.  
  160. (setf *album* (make-album "Miles Davis"
  161.                           "Bitches Brew"
  162.                           (list (make-track "Pharaoh's Dance" (make-time 20 05))                                                       (make-track "Bitches Brew" (make-time 26 58))
  163.                                 (make-track "Spanish Key" (make-time 17 32))
  164.                                 (make-track "John McLaughlin" (make-time 4 22))
  165.                                 (make-track "Miles Runs the Voodoo Down" (make-time 14 01))
  166.                                 (make-track "Sanctuary" (make-time 10 56))
  167.                                 (make-track "Feio" (make-time 11 49)))
  168.                           1969))
Add Comment
Please, Sign In to add comment