-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterate-advanced-examples.lisp
130 lines (108 loc) · 4.1 KB
/
iterate-advanced-examples.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;; This file both collects example code from the Iterate manual, adds
;; examples for constructs without exemplars and has several advanced
;; examples of drivers and generators.
;; Getting started -- load iterate from Quicklisp and import all
;; exported symbols into your current package. iterate-keywords
;; (separate from Common Lisp keywords) are symbols rather than
;; designators of some kind and therefor must be refered to by their
;; package name if they aren't imported, something that quickly
;; becomes tedious, e.g.
;;
;; (itererate::iter (itererate::for i itererate::from 1 itererate::to 10)
;; (itererate::collect i))
;;
;; For more information on installing quicklisp see the main website:
;;
;; https://www.quicklisp.org/
(ql:quickload :iterate)
(use-package :iterate)
;; 1
;;
;; simple example
(iter (for i from 1 to 10)
(collect i))
;; >> (1 2 3 4 5 6 7 8 9 10)
;; collect odd numbers in a list
(let ((list '(1 2 3 4 5 6 7 8 9 10)))
(iter (for el in list)
(if (and (numberp el) (oddp el))
(collect el))))
;; >> (1 3 5 7 9)
;; Example of iterating over alist, of creation a new variable
;; bindings, stepping over multiple sequences at once, using compiler
;; declarations of variable types, and collecting values
(let ((alist '((a 1) (b 2) (c 3) (d 4))))
(iter (for (key . item) in alist)
(for i from 0)
(declare (fixnum i))
(collect (cons i key))))
;; >> ((0 . A) (1 . B) (2 . C) (3 . D))
;; 2.1 Drivers
;;
;; Example of repeat, the only iteration driver clause that doesn't
;; begin with for.
(iter (repeat 3)
(print "I will not talk in class."))
;; >> "I will not talk in class."
;; >> "I will not talk in class."
;; >> "I will not talk in class."
;; 2.1.2 Sequence iteration
;;
;; for var in list &optional by step-function
;; Print every item in a list (from iterate-examples.lisp)
(iterate (for item in '(1 2 3))
(print item))
;; >> 1
;; >> 2
;; >> 3
;; => NIL
;; Print every other item in a list. (from iterate-examples.lisp)
(iterate (for item in '(1 2 3 4 5) by #'cddr)
(print item))
;; >> 1
;; >> 3
;; >> 5
;; => NIL
;; for var on list &optional by step-function
;; Collect successive tails of a list. (from iterate-examples.lisp)
(iterate (for sublist on '(a b c d))
(collect sublist))
;; => ((A B C D) (B C D) (C D) (D))
;; Search list for symbol and return subsequent item.
(iterate (for sublist on '(:a 1 :b 2 :c 3) by #'cddr)
(when (eq (car sublist) :b)
(return (cadr sublist))))
;; => 2
;; for var in-vector vector &sequence
;; &sequence := from | upfrom | downfrom | to | downto | above | below | by
;; Print sequence of letters
(let ((letters (vector 'a 'b 'c 'd 'e 'f)))
(iterate (for letter in-vector letters)
(format t "~s~%" letter)))
;; Print sequence of letters using from and downto forms, i.e. from
;; 'z' down to 'u' (zero-based) character in the alphabet, specifying
;; that the index variable be lexically bound to i.
(let ((letters "abcdefghijklmnopqrstuvwxyz"))
(iterate (for letter in-vector letters with-index i from 25 downto 20)
(format t "~s: ~a~%" i (string letter))))
;; likewise except using upfrom, characters in the interval ['u', 'z']
(let ((letters "abcdefghijklmnopqrstuvwxyz"))
(iterate (for letter in-vector letters with-index i upfrom 20)
(format t "~s: ~a~%" i (string letter))))
;; likewise except using downfrom, characters in the interval ['k',
;; 'a']
(let ((letters "abcdefghijklmnopqrstuvwxyz"))
(iterate (for letter in-vector letters with-index i downfrom 10)
(format t "~s: ~a~%" i (string letter))))
;; Likewise except characters in the interval [p, q], note from and
;; upfrom are synonyms
(let ((letters "abcdefghijklmnopqrstuvwxyz"))
(iterate (for letter in-vector letters with-index i from 15)
(format t "~s: ~a~%" i (string letter))))
(let ((letters "abcdefghijklmnopqrstuvwxyz"))
(iterate (for letter in-vector letters with-index i upfrom 15)
(format t "~s: ~a~%" i (string letter))))
;; likewise
(let ((letters "abcdefghijklmnopqrstuvwxyz"))
(iterate (for letter in-vector letters with-index i from 15 downto 5)
(format t "~s: ~a~%" i (string letter))))