home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / CLISP-1.LHA / CLISP960530-sr.lha / tests / loop.tst < prev    next >
Encoding:
Text File  |  1996-04-17  |  13.8 KB  |  709 lines

  1. (loop for x from 1 to 9
  2.       for y = nil then x
  3.       collect (list x y)
  4. )
  5. ((1 NIL) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9))
  6.  
  7. (loop for x from 1 to 9
  8.       and y = nil then x
  9.       collect (list x y)
  10. )
  11. ((1 NIL) (2 1) (3 2) (4 3) (5 4) (6 5) (7 6) (8 7) (9 8))
  12.  
  13. (with-output-to-string (*standard-output*)
  14.   (loop as i from 1 to 5
  15.         do (print i)
  16. ) )
  17. "
  18. 5 "
  19.  
  20. (with-output-to-string (*standard-output*)
  21.   (loop for i from 10 downto 1 by 3
  22.         do (print i)
  23. ) )
  24. "
  25. 10 
  26. 1 "
  27.  
  28. (with-output-to-string (*standard-output*)
  29.   (loop as i below 5
  30.         do (print i)
  31. ) )
  32. "
  33. 4 "
  34.  
  35. (with-output-to-string (*standard-output*)
  36.   (loop for item in '(1 2 3 4 5)
  37.         do (print item)
  38. ) )
  39. "
  40. 5 "
  41.  
  42. (with-output-to-string (*standard-output*)
  43.   (loop for item in '(1 2 3 4 5) by #'cddr
  44.         do (print item)
  45. ) )
  46. "
  47. 5 "
  48.  
  49. (loop for (item . x) (t . fixnum) in '((A . 1) (B . 2) (C . 3))
  50.       unless (eq item 'B) sum x
  51. )
  52. 4
  53.  
  54. (loop for sublist on '(a b c d)
  55.       collect sublist
  56. )
  57. ((A B C D) (B C D) (C D) (D))
  58.  
  59. (with-output-to-string (*standard-output*)
  60.   (loop for (item) on '(1 2 3)
  61.         do (print item)
  62. ) )
  63. "
  64. 3 "
  65.  
  66. (with-output-to-string (*standard-output*)
  67.   (loop for item in '(1 2 3)
  68.         do (print item)
  69. ) )
  70. "
  71. 3 "
  72.  
  73. (loop for i below 5
  74.       for j = 10 then i
  75.       collect j
  76. )
  77. (10 1 2 3 4)
  78.  
  79. (loop for i below 5
  80.       for j = i
  81.       collect j
  82. )
  83. (0 1 2 3 4)
  84.  
  85. (loop for item = 1 then (+ item 10)
  86.       repeat 5
  87.       collect item
  88. )
  89. (1 11 21 31 41)
  90.  
  91. (loop for char across (the simple-string "Hello")
  92.       collect char
  93. )
  94. (#\H #\e #\l #\l #\o)
  95.  
  96. (with-output-to-string (*standard-output*)
  97.   (loop repeat 3
  98.         do (write-line "What I say three times is true")
  99. ) )
  100. "What I say three times is true
  101. What I say three times is true
  102. What I say three times is true
  103. "
  104.  
  105. (with-output-to-string (*standard-output*)
  106.   (loop repeat -15
  107.         do (write-line "What you see is what you expect")
  108. ) )
  109. ""
  110.  
  111. #| ;; FOR clauses should come before WHILE clauses
  112. (let ((stack '(a b c d e f)))
  113.   (loop while stack
  114.         for item = (length stack) then (pop stack)
  115.         collect item
  116. ) )
  117. (6 A B C D E F)
  118. |#
  119.  
  120. (loop for i fixnum from 3
  121.       when (oddp i) collect i
  122.       while (< i 5)
  123. )
  124. (3 5)
  125.  
  126. (loop for i from 0 to 10
  127.       always (< i 11)
  128. )
  129. T
  130.  
  131. (loop for i from 0 to 10
  132.       never (> i 11)
  133. )
  134. T
  135.  
  136. (loop for i from 0
  137.       thereis (when (> i 10) i)
  138. )
  139. 11
  140.  
  141. (with-output-to-string (*standard-output*)
  142.   (loop for i from 0 to 10
  143.         always (< i 9)
  144.         finally (print "You won't see this")
  145. ) )
  146. ""
  147.  
  148. (with-output-to-string (*standard-output*)
  149.   (loop never t
  150.         finally (print "You won't see this")
  151. ) )
  152. ""
  153.  
  154. (with-output-to-string (*standard-output*)
  155.   (loop thereis "Here is my value"
  156.         finally (print "You won't see this")
  157. ) )
  158. ""
  159.  
  160. (loop thereis "Here is my value"
  161.       finally (print "You won't see this")
  162. )
  163. "Here is my value"
  164.  
  165. (with-output-to-string (*standard-output*)
  166.   (loop for i from 1 to 10
  167.         thereis (> i 11)
  168.         finally (print i)
  169. ) )
  170. "
  171. 11 "
  172.  
  173. (let (everest chocorua sahara)
  174.   (defstruct mountain  height difficulty (why "because it is there"))
  175.   (setq everest (make-mountain :height '(2.86e-13 parsecs)))
  176.   (setq chocorua (make-mountain :height '(1059180001 microns)))
  177.   (defstruct desert  area (humidity 0))
  178.   (setq sahara (make-desert :area '(212480000 square furlongs)))
  179.   (loop for x in (list everest sahara chocorua)
  180.         thereis (and (mountain-p x) (mountain-height x))
  181. ) )
  182. (2.86e-13 parsecs)
  183.  
  184. (with-output-to-string (*standard-output*)
  185.   (loop for (month date-list) in '((january (24 28)) (february (17 29 12)))
  186.         do (loop for date in date-list
  187.                  do (case date
  188.                       (29 (when (eq month 'february) (loop-finish)))
  189.                     )
  190.                  do (format t "~:(~A~) ~A~%" month date)
  191. ) )        )
  192. "January 24
  193. January 28
  194. February 17
  195. "
  196.  
  197. (loop for i in '(1 2 3 stop-here 4 5 6)
  198.       when (symbolp i) do (loop-finish)
  199.       count i
  200. )
  201. 3
  202.  
  203. (loop for i in '(1 2 3 stop-here 4 5 6)
  204.       until (symbolp i)
  205.       count i
  206. )
  207. 3
  208.  
  209. (loop for name in '(fred sue alice joe june)
  210.       for kids in '((bob ken) () () (kris sunshine) ())
  211.       collect name
  212.       append kids
  213. )
  214. (FRED BOB KEN SUE ALICE JOE KRIS SUNSHINE JUNE)
  215.  
  216. (multiple-value-list
  217.   (loop for name in '(fred sue alice joe june)
  218.         as age in '(22 26 19 20 10)
  219.         append (list name age) into name-and-age-list
  220.         count name into name-count
  221.         sum age into total-age
  222.         finally
  223.           (return (values (round total-age name-count) name-and-age-list))
  224. ) )
  225. (19 (FRED 22 SUE 26 ALICE 19 JOE 20 JUNE 10))
  226.  
  227. (loop for i in '(bird 3 4 turtle (1 . 4) horse cat)
  228.       when (symbolp i) collect i
  229. )
  230. (BIRD TURTLE HORSE CAT)
  231.  
  232. (loop for i from 1 to 10
  233.       if (oddp i) collect i
  234. )
  235. (1 3 5 7 9)
  236.  
  237. (with-output-to-string (*standard-output*)
  238.   (loop for i in '(a b c d) by #'cddr
  239.         collect i into my-list
  240.         finally (print my-list)
  241. ) )
  242. "
  243. (A C) "
  244.  
  245. (loop for x in '((a) (b) ((c)))
  246.       append x
  247. )
  248. (A B (C))
  249.  
  250. (loop for i upfrom 0
  251.       as x in '(a b (c))
  252.       nconc (if (evenp i) (list x) '())
  253. )
  254. (A (C))
  255.  
  256. (loop for i in '(a b nil c nil d e)
  257.       count i
  258. )
  259. 5
  260.  
  261. (loop for i fixnum in '(1 2 3 4 5)
  262.       sum i
  263. )
  264. 15
  265.  
  266. (let ((series '(1.2 4.3 5.7)))
  267.   (loop for v in series
  268.         sum (* 2.0 v)
  269. ) )
  270. 22.4
  271.  
  272. (loop for i in '(2 1 5 3 4)
  273.       maximize i
  274. )
  275. 5
  276.  
  277. (loop for i in '(2 1 5 3 4)
  278.       minimize i
  279. )
  280. 1
  281.  
  282. (let ((series '(1.2 4.3 5.7)))
  283.   (loop for v in series
  284.         maximize (round v) fixnum
  285. ) )
  286. 6
  287.  
  288. (let ((series '(1.2 4.3 5.7)))
  289.   (loop for v in series
  290.         minimize (round v) into result fixnum
  291.         finally (return result)
  292. ) )
  293. 1
  294.  
  295. (loop with a = 1
  296.       with b = (+ a 2)
  297.       with c = (+ b 3)
  298.       with d = (+ c 4)
  299.       return (list a b c d)
  300. )
  301. (1 3 6 10)
  302.  
  303. (loop with a = 1
  304.        and b = 2
  305.        and c = 3
  306.        and d = 4
  307.       return (list a b c d)
  308. )
  309. (1 2 3 4)
  310.  
  311. (let ((a 5) (b 10) (c 1729))
  312.   (loop with a = 1
  313.          and b = (+ a 2)
  314.          and c = (+ b 3)
  315.          and d = (+ c 4)
  316.         return (list a b c d)
  317. ) )
  318. (1 7 13 1733)
  319.  
  320. (loop with (a b c) (float integer float)
  321.       return (format nil "~A ~A ~A" a b c)
  322. )
  323. "0.0 0 0.0"
  324.  
  325. (loop with (a b c) float
  326.       return (format nil "~A ~A ~A" a b c)
  327. )
  328. "0.0 0.0 0.0"
  329.  
  330. (let ((numbers-list '(3 2 4 6 1 7 8)) (results nil))
  331.   (cons
  332.     (with-output-to-string (*standard-output*)
  333.       (loop for i in numbers-list
  334.             when (oddp i)
  335.               do (print i)
  336.               and collect i into odd-numbers
  337.               and do (terpri)
  338.               else
  339.               collect i into even-numbers
  340.             finally (setq results (list odd-numbers even-numbers))
  341.     ) )
  342.     results
  343. ) )
  344. ("
  345.  
  346.  
  347. "
  348. (3 1 7) (2 4 6 8))
  349.  
  350. (loop for i in '(1 2 3 4 5 6)
  351.       when (and (> i 3) i)
  352.         collect it
  353. )
  354. (4 5 6)
  355.  
  356. (loop for i in '(1 2 3 4 5 6)
  357.       when (and (> i 3) i)
  358.         return it
  359. )
  360. 4
  361.  
  362. (loop for i in '(1 2 3 4 5 6)
  363.       thereis (and (> i 3) i)
  364. )
  365. 4
  366.  
  367. (with-output-to-string (*standard-output*)
  368.   (loop for x from 0 to 3
  369.         do (print x)
  370.         if (zerop (mod x 2))
  371.           do (write-string " a")
  372.           and
  373.           if (zerop (floor x 2))
  374.             do (write-string " b")
  375.             and
  376.             do (write-string " c")
  377. ) )
  378. "
  379. 0  a b c
  380. 2  a
  381. 3 "
  382.  
  383. (with-output-to-string (*standard-output*)
  384.   (loop for x from 0 to 3
  385.         do (print x)
  386.         if (zerop (mod x 2))
  387.           do (write-string " a")
  388.           and
  389.           if (zerop (floor x 2))
  390.             do (write-string " b")
  391.             end
  392.           and
  393.           do (write-string " c")
  394. ) )
  395. "
  396. 0  a b c
  397. 2  a c
  398. 3 "
  399.  
  400. (with-output-to-string (*standard-output*)
  401.   (loop for i from 1 to 5
  402.         do (print i)
  403. ) )
  404. "
  405. 5 "
  406.  
  407. (with-output-to-string (*standard-output*)
  408.   (loop for i from 1 to 4
  409.         do (print i)
  410.            (print (* i i))
  411. ) )
  412. "
  413. 16 "
  414.  
  415. (loop for item in '(1 2 3 a 4 5)
  416.       when (not (numberp item))
  417.         return (format nil "non-numeric value: ~S" item)
  418. )
  419. "non-numeric value: A"
  420.  
  421. (loop for item in '(1 2 3 a 4 5)
  422.       when (not (numberp item))
  423.         do (return (format nil "non-numeric value: ~S" item))
  424. )
  425. "non-numeric value: A"
  426.  
  427. (loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  428.       for a integer = (first numlist)
  429.       for b integer = (second numlist)
  430.       for c float = (third numlist)
  431.       collect (list c b a)
  432. )
  433. ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  434.  
  435. (loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  436.       for a integer = (first numlist)
  437.       and for b integer = (second numlist)
  438.       and for c float = (third numlist)
  439.       collect (list c b a)
  440. )
  441. ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  442.  
  443. (loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  444.       for a integer = (first numlist)
  445.       and b integer = (second numlist)
  446.       and c float = (third numlist)
  447.       collect (list c b a)
  448. )
  449. ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  450.  
  451. (loop for (a b c) (integer integer float) in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  452.       collect (list c b a)
  453. )
  454. ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  455.  
  456. (loop for (a b c) float in '((1.0 2.0 4.0) (5.0 6.0 8.3) (8.0 9.0 10.4))
  457.       collect (list c b a)
  458. )
  459. ((4.0 2.0 1.0) (8.3 6.0 5.0) (10.4 9.0 8.0))
  460.  
  461. (loop with (a b) float = '(1.0 2.0)
  462.       and (c d) integer = '(3 4)
  463.       and (e f)
  464.       return (list a b c d e f)
  465. )
  466. (1.0 2.0 3 4 NIL NIL)
  467.  
  468. (loop for (a nil b) = '(1 2 3)
  469.       do (return (list a b))
  470. )
  471. (1 3)
  472.  
  473. (loop for (x . y) = '(1 . 2)
  474.       do (return y)
  475. )
  476. 2
  477.  
  478. (loop for ((a . b) (c . d)) of-type ((float . float) (integer . integer))
  479.                             in '(((1.2 . 2.4) (3 . 4)) ((3.4 . 4.6) (5 . 6)))
  480.       collect (list a b c d)
  481. )
  482. ((1.2 2.4 3 4) (3.4 4.6 5 6))
  483.  
  484. (loop for buffer in '("\"Hello\"" "\"unterminated" "nothing")
  485.       collect
  486.         (loop initially (unless (char= (char buffer 0) #\") (loop-finish))
  487.               for i fixnum from 1 below (length buffer)
  488.               when (char= (char buffer i) #\")
  489.                 return i
  490. )       )
  491. (6 NIL NIL)
  492.  
  493. (let (result)
  494.   (list
  495.     (with-output-to-string (*standard-output*)
  496.       (setq result
  497.         (loop for i from 1 to 10
  498.               when (> i 5)
  499.                 collect i
  500.               finally (print i)
  501.     ) ) )
  502.     result
  503. ) )
  504. ("
  505. 11 " (6 7 8 9 10))
  506.  
  507. (multiple-value-list
  508.   (loop for i from 1 to 10
  509.         when (> i 5)
  510.           collect i into number-list
  511.           and count i into number-count
  512.         finally (return (values number-count number-list))
  513. ) )
  514. (5 (6 7 8 9 10))
  515.  
  516. (let (result)
  517.   (list
  518.     (with-output-to-string (*standard-output*)
  519.       (setq result
  520.         (loop named max
  521.               for i from 1 to 10
  522.               do (print i)
  523.               do (return-from max 'done)
  524.     ) ) )
  525.     result
  526. ) )
  527. ("
  528. 1 " DONE)
  529.  
  530. (loop for i = 0 for j to 2 collect j)
  531. (0 1 2)
  532.  
  533. (loop for i in '(1 2) for j = i for k = j collect (list i j k))
  534. ((1 1 1) (2 2 2))
  535.  
  536. (loop for idx upfrom 0 below 5 for char = (aref "Error" idx) collect char)
  537. (#\E #\r #\r #\o #\r)
  538.  
  539. (let ((hash-table (make-hash-table)))
  540.   (setf (gethash 1 hash-table) 100)
  541.   (setf (gethash 2 hash-table) 200)
  542.   (sort
  543.    (loop for key being each hash-key in hash-table using (hash-value val)
  544.          for key+1 = (1+ key)
  545.          collect (list key key+1 val))
  546.    #'<
  547.    :key #'car))
  548. ((1 2 100) (2 3 200))
  549.  
  550. (loop for i across #(1 2 3 4) for j = (1+ i) collect (list i j))
  551. ((1 2) (2 3) (3 4) (4 5))
  552.  
  553. (loop for i in '() for j = (1+ i) collect j)
  554. nil
  555.  
  556. (loop for i across #() for j = (1+ i) collect j)
  557. nil
  558.  
  559. (loop for x = t for y IN '(A B C) for z = t collect y)
  560. (A B C)
  561.  
  562. (loop for x = t for y across #(A B C) for z = t collect y)
  563. (A B C)
  564.  
  565. (loop for x = t for y in () for z = t collect y)
  566. nil
  567.  
  568. (loop for x = t for y across #() for z = t collect y)
  569. nil
  570.  
  571. (let ((hash-table (make-hash-table)))
  572.   (setf (gethash 1 hash-table) 100)
  573.   (setf (gethash 2 hash-table) 200)
  574.   (sort
  575.    (loop for x = t
  576.          for key being each hash-key in hash-table using (hash-value val)
  577.          for key+1 = (1+ key)
  578.          for z = t
  579.          collect (list key key+1 val))
  580.    #'<
  581.    :key #'car))
  582. ((1 2 100) (2 3 200))
  583.  
  584. (loop for i from 1 to 0 collect i)
  585. nil
  586.  
  587. (let ((hash-table (make-hash-table)))
  588.   (setf (gethash 1 hash-table) 100)
  589.   (setf (gethash 2 hash-table) 200)
  590.   (sort
  591.    (loop for val being each hash-value in hash-table
  592.          collect val)
  593.    #'<))
  594. (100 200)
  595.  
  596. (let ((hash-table (make-hash-table)))
  597.   (setf (gethash 1 hash-table) 100)
  598.   (setf (gethash 2 hash-table) 200)
  599.   (sort
  600.    (loop for val being each hash-value in hash-table for deriv-val = (/ 1 val) 
  601.          collect deriv-val)
  602.    #'<))
  603. (1/200 1/100)
  604.  
  605. (let ((hash-table (make-hash-table)))
  606.   (setq i 123456789)
  607.   (setf (gethash 1 hash-table) 100)
  608.   (setf (gethash 2 hash-table) 200)
  609.   (loop for i across #(1 2 3 4 5 6) collect i)
  610.   (loop for i in '(1 2 3 4 5 6) collect i)
  611.   (loop for i being each hash-key of hash-table collect i)
  612.   (loop for i being each present-symbol of *package* collect i)
  613.   i)
  614. 123456789
  615.  
  616. (loop for x on '(3 4 5)
  617.       for y = (car x)
  618.       for z in '(a b c)
  619.       collect z)
  620. (a b c)
  621.  
  622. (loop for x across #(3 4 5)
  623.       for y = (1+ x)
  624.       for z across #(a b c)
  625.       collect (list x y z))
  626. ((3 4 a) (4 5 b) (5 6 c))
  627.  
  628. (loop for x across #()
  629.       for y = x
  630.       for z across #(a b c)
  631.       collect (list x y z))
  632. nil
  633.  
  634. (loop for x across #(1 2 3)
  635.       for y = x
  636.       for z across #()
  637.       collect (list x y z))
  638. nil
  639.  
  640. (loop for x across #(1 2 3)
  641.       for y = (1+ x)
  642.       for z across #(a b)
  643.       collect (list x y z))
  644. ((1 2 a) (2 3 b))
  645.  
  646. (loop for x across #(1 2)
  647.       for y = (1+ x)
  648.       for z across #(a b c)
  649.       collect (list x y z))
  650. ((1 2 a) (2 3 b))
  651.  
  652. (let ((package (make-package "LOOP-TEST")))
  653.   (intern "blah" package)
  654.   (let ((blah2 (intern "blah2" package)))
  655.     (export blah2 package))
  656.   (list
  657.    (sort 
  658.     (loop for sym being each present-symbol of package 
  659.           for sym-name = (symbol-name sym)
  660.           collect sym-name)
  661.     #'string<)
  662.    (sort 
  663.     (loop for sym being each external-symbol of package 
  664.           for sym-name = (symbol-name sym)
  665.           collect sym-name)
  666.     #'string<)))
  667. (("blah" "blah2") ("blah2"))
  668.  
  669. (let ((ht (make-hash-table)))
  670.   (loop for key being each hash-key of ht
  671.         for value = (gethash key ht)
  672.         collect (list key value)))
  673. nil