Skip to content

Instantly share code, notes, and snippets.

@miyamuko
Created August 6, 2010 10:17
Show Gist options
  • Select an option

  • Save miyamuko/511137 to your computer and use it in GitHub Desktop.

Select an option

Save miyamuko/511137 to your computer and use it in GitHub Desktop.
CMUCL の loop マクロを xyzzy に移植したもので実践 Common Lisp のサンプルを動かして結果
;; CMUCL の loop マクロを xyzzy に移植したもので実践 Common Lisp のサンプルを動かして結果。
;; collect it を含めて一応全部動いているみたい。
;; http://gist.github.com/511132
(loop for i upto 10 collect i)
(0 1 2 3 4 5 6 7 8 9 10)
(loop for i from 0 downto -10 collect i)
(0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10)
(loop for i from 20 downto 10 collect i)
(20 19 18 17 16 15 14 13 12 11 10)
(loop for i downfrom 20 to 10 collect i)
(20 19 18 17 16 15 14 13 12 11 10)
(loop repeat 3 do (msgbox "hello"))
(loop for i in (list 10 20 30 40) collect i)
(10 20 30 40)
(loop for i in (list 10 20 30 40) by #'cddr collect i)
(10 30)
(loop for x on (list 10 20 30 40) collect x)
((10 . #1=(20 . #2=(30 . #3=(40)))) #1# #2# #3#)
(loop for x on (list 10 20 30 40) by #'cddr collect x)
((10 20 . #1=(30 40)) #1#)
(loop for x across "abcd" collect x)
(#\a #\b #\c #\d)
(setf *hash* (let ((h (make-hash-table)))
(mapc #'(lambda (c)
(setf (gethash c h) (char-unicode c)))
(list #\a #\b #\c #\あ #\い #\う))
h))
(loop for k being the hash-keys in *hash* collect k)
(#\あ #\い #\う #\a #\b #\c)
(loop for v being the hash-values in *hash* collect v)
(12354 12356 12358 97 98 99)
(loop for k being the hash-keys in *hash* using (hash-value v)
collect (format nil "~A => ~A" k v))
("あ => 12354" "い => 12356" "う => 12358" "a => 97" "b => 98" "c => 99")
(loop for v being the hash-values in *hash* using (hash-key k)
collect (format nil "~A => ~A" k v))
("あ => 12354" "い => 12356" "う => 12358" "a => 97" "b => 98" "c => 99")
(loop repeat 5
for x = 0 then y
for y = 1 then (+ x y)
collect y)
(1 2 4 8 16)
(loop repeat 5
for y = 1 then (+ x y)
for x = 0 then y
collect y)
(1 1 2 4 8)
(loop repeat 5
for x = 0 then y
and y = 1 then (+ x y)
collect y)
(1 1 2 3 5)
(loop with x1 = 10 and y1 = 20
repeat 5
for x = x1 then y
and y = y1 then (+ x y)
collect y)
(20 30 50 80 130)
(loop for (a b) in '((1 2) (3 4) (5 6))
do (format t "a: ~A; b: ~A~%" a b))
a: 1; b: 2
a: 3; b: 4
a: 5; b: 6
nil
(loop for (item . rest) on '(1 2 3 4 5)
do (format t "~A" item)
when rest do (format t ", "))
1, 2, 3, 4, 5
nil
(loop for (a nil) in '((1 2) (3 4) (5 6)) collect a)
(1 3 5)
(defparameter *random* (loop repeat 100 collect (random 10000)))
*random*
(loop for i in *random*
counting (evenp i) into evens
counting (oddp i) into odds
summing i into total
maximizing i into max
minimizing i into min
finally (return (list min max total evens odds)))
(4 9921 486289 57 43)
(loop for i from 1 to 10 do (print i))
1
2
3
4
5
6
7
8
9
10
nil
(block outer
(loop for i from 0 return 100)
(print "This will print")
200)
"This will print"
200
(block outer
(loop for i from 0 do (return-from outer 100))
(print "This won't print")
200)
100
(loop for i from 1 to 10 do (when (evenp i) (print i)))
2
4
6
8
10
nil
(loop for i from 1 to 10 when (evenp i) sum i)
30
(loop for key across "abcdefghijklmnopqrstuvwxyz" when (lookup-keymap ctl-x-6-map key)
collect it)
(load-session close-session other-pseudo-frame previous-pseudo-frame save-session)
(defvar *analysis* nil)
(defun update-analysis (&rest args)
(setf *analysis* args))
(loop for i from 1 to 100
if (evenp i)
minimize i into min-even and
maximize i into max-even and
unless (zerop (mod i 4))
sum i into even-not-fours-total
end
and sum i into even-total
else
minimize i into min-odd and
maximize i into max-odd and
unless (zerop (mod i 5))
sum i into fives-total
end
and sum i into odd-total
do (update-analysis min-even max-even min-odd max-odd even-total odd-total fives-total even-not-fours-total))
nil
*analysis*
(2 100 1 99 2550 2500 2000 1250)
(loop named outer for list in '((1 2 3 4) (1 3 5 7 9)) do
(loop for item in list do
(if (evenp item)
(return-from outer item))))
2
(if (loop for n in '(2 4 6 8) always (evenp n))
(print "All numbers even."))
"All numbers even."
"All numbers even."
(if (loop for n in '(1 3 5) always (evenp n))
(print "All numbers even."))
nil
(if (loop for n in '(2 4 6 8) never (oddp n))
(print "All numbers even."))
"All numbers even."
"All numbers even."
(if (loop for n in '(1 3 5) never (oddp n))
(print "All numbers even."))
nil
(loop for char across "abc123" thereis (digit-char-p char))
1
(loop for char across "abcdef" thereis (digit-char-p char))
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment