fork download
  1. (defun f (A |a| B)
  2. (let* ((i (position |a| A))
  3. (A2 (nconc (subseq A i) (subseq A 0 i))))
  4. (sort (copy-seq B) (lambda (x y) (< (position x A2) (position y A2))))))
  5.  
  6. (defun g (A |a| B)
  7. (let ((i (position |a| A)))
  8. (loop with result = '()
  9. for x in (nconc (subseq A i) (subseq A 0 i))
  10. when (member x B)
  11. do (loop repeat (count x B) do (push x result))
  12. finally (return (nreverse result)))))
  13.  
  14. (dolist (fn '(f g))
  15. (format t "~S:~%" fn)
  16. (loop for args in '(((2 3 5 7 9) 2 (9 2 5))
  17. ((2 3 5 7 9) 9 (9 2 5))
  18. ((2 3 5 7 9) 5 (9 2 5))
  19. ((2 3 5 7 9) 3 (9 2 5 5 2)))
  20. do (format t "~{A = ~S, a = ~S, B = ~S~} ~42T => ~S~%"
  21. args
  22. (apply fn args)))
  23. (terpri))
  24.  
  25. (defmacro b (n exp)
  26. `(progn
  27. (write-line "==================================================")
  28. (time (loop repeat n do ,exp
  29. finally (format t "Evaluated ~S ~D times." ',exp ,n)))))
  30.  
  31. (compile 'f)
  32. (compile 'g)
  33. (let ((n 80000))
  34. (b n (f '(2 3 5 7 9) 9 '(7 3 3 5 2 2 2 7 7 5 3 9)))
  35. (b n (g '(2 3 5 7 9) 9 '(7 3 3 5 2 2 2 7 7 5 3 9))))
  36.  
Success #stdin #stdout 4.4s 10816KB
stdin
Standard input is empty
stdout
F:
A = (2 3 5 7 9), a = 2, B = (9 2 5)        =>  (2 5 9)
A = (2 3 5 7 9), a = 9, B = (9 2 5)        =>  (9 2 5)
A = (2 3 5 7 9), a = 5, B = (9 2 5)        =>  (5 9 2)
A = (2 3 5 7 9), a = 3, B = (9 2 5 5 2)    =>  (5 5 9 2 2)

G:
A = (2 3 5 7 9), a = 2, B = (9 2 5)        =>  (2 5 9)
A = (2 3 5 7 9), a = 9, B = (9 2 5)        =>  (9 2 5)
A = (2 3 5 7 9), a = 5, B = (9 2 5)        =>  (5 9 2)
A = (2 3 5 7 9), a = 3, B = (9 2 5 5 2)    =>  (5 5 9 2 2)

==================================================
Evaluated (F '(2 3 5 7 9) 9 '(7 3 3 5 2 2 2 7 7 5 3 9)) 80000 times.
Real time: 3.411233 sec.
Run time: 3.399483 sec.
Space: 18571096 Bytes
GC: 35, GC time: 0.084986 sec.
==================================================
Evaluated (G '(2 3 5 7 9) 9 '(7 3 3 5 2 2 2 7 7 5 3 9)) 80000 times.
Real time: 0.97171 sec.
Run time: 0.967853 sec.
Space: 10891096 Bytes
GC: 21, GC time: 0.054988 sec.