DEV Community

loading...
Cover image for Programming in Clojure (Part 5 Laziness)

Programming in Clojure (Part 5 Laziness)

Mark Mahoney
CS professor
Updated on ・6 min read

This post covers creating lazy sequences in Clojure. A lazy sequence defers the cost of creating values until they are needed.

Lazy sequences

The first program shows how to create a lazy sequence. The next two are more concrete examples.

Call to Action

Problem 1
Create a function that generates a lazy sequence of squares. For example, (1 4 9 16 ... to infinity).

Then write a function that generates a lazy sequence of values raised to a power:

(defn lazy-pow [start-val power]
  ...)

(take 6 (lazy-pow 10 2)) ;(100 121 144 169 196 225)
(take 6 (lazy-pow 10 3)) ;(1000 1331 1728 2197 2744 3375)
Enter fullscreen mode Exit fullscreen mode

Problem 2
Write a function that generates an infinite lazy sequence of possible permutations of the passed in sequence.

(take 3 (lazy-perm ["a" "b" "c"]))
;("a" "b" "c")

(take 12 (lazy-perm ["a" "b" "c"])) 
;("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc")

(take 39 (lazy-perm ["a" "b" "c"])) 
;("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba" "bbb" "bbc" "bca" "bcb" "bcc" "caa" "cab" "cac" "cba" "cbb" "cbc" "cca" "ccb" "ccc")

(take 120 (lazy-perm ["a" "b" "c"]))
;("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba" "bbb" "bbc" "bca" "bcb" "bcc" "caa" "cab" "cac" "cba" "cbb" "cbc" "cca" "ccb" "ccc" "aaaa" "aaab" "aaac" "aaba" "aabb" "aabc" "aaca" "aacb" "aacc" "abaa" "abab" "abac" "abba" "abbb" "abbc" "abca" "abcb" "abcc" "acaa" "acab" "acac" "acba" "acbb" "acbc" "acca" "accb" "accc" "baaa" "baab" "baac" "baba" "babb" "babc" "baca" "bacb" "bacc" "bbaa" "bbab" "bbac" "bbba" "bbbb" "bbbc" "bbca" "bbcb" "bbcc" "bcaa" "bcab" "bcac" "bcba" "bcbb" "bcbc" "bcca" "bccb" "bccc" "caaa" "caab" "caac" "caba" "cabb" "cabc" "caca" "cacb" "cacc" "cbaa" "cbab" "cbac" "cbba" "cbbb" "cbbc" "cbca" "cbcb" "cbcc" "ccaa" "ccab" "ccac" "ccba" "ccbb" "ccbc" "ccca" "cccb" "cccc")

(take 1000 (lazy-perm ["a" "b" "c"])) 
;("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba" "bbb" "bbc" "bca" "bcb" "bcc" "caa" "cab" "cac" "cba" "cbb" "cbc" "cca" "ccb" "ccc" "aaaa" "aaab" "aaac" "aaba" "aabb" "aabc" "aaca" "aacb" "aacc" "abaa" "abab" "abac" "abba" "abbb" "abbc" "abca" "abcb" "abcc" "acaa" "acab" "acac" "acba" "acbb" "acbc" "acca" "accb" "accc" "baaa" "baab" "baac" "baba" "babb" "babc" "baca" "bacb" "bacc" "bbaa" "bbab" "bbac" "bbba" "bbbb" "bbbc" "bbca" "bbcb" "bbcc" "bcaa" "bcab" "bcac" "bcba" "bcbb" "bcbc" "bcca" "bccb" "bccc" "caaa" "caab" "caac" "caba" "cabb" "cabc" "caca" "cacb" "cacc" "cbaa" "cbab" "cbac" "cbba" "cbbb" "cbbc" "cbca" "cbcb" "cbcc" "ccaa" "ccab" "ccac" "ccba" "ccbb" "ccbc" "ccca" "cccb" "cccc" "aaaaa" "aaaab" "aaaac" "aaaba" "aaabb" "aaabc" "aaaca" "aaacb" "aaacc" "aabaa" "aabab" "aabac" "aabba" "aabbb" "aabbc" "aabca" "aabcb" "aabcc" "aacaa" "aacab" "aacac" "aacba" "aacbb" "aacbc" "aacca" "aaccb" "aaccc" "abaaa" "abaab" "abaac" "ababa" "ababb" "ababc" "abaca" "abacb" "abacc" "abbaa" "abbab" "abbac" "abbba" "abbbb" "abbbc" "abbca" "abbcb" "abbcc" "abcaa" "abcab" "abcac" "abcba" "abcbb" "abcbc" "abcca" "abccb" "abccc" "acaaa" "acaab" "acaac" "acaba" "acabb" "acabc" "acaca" "acacb" "acacc" "acbaa" "acbab" "acbac" "acbba" "acbbb" "acbbc" "acbca" "acbcb" "acbcc" "accaa" "accab" "accac" "accba" "accbb" "accbc" "accca" "acccb" "acccc" "baaaa" "baaab" "baaac" "baaba" "baabb" "baabc" "baaca" "baacb" "baacc" "babaa" "babab" "babac" "babba" "babbb" "babbc" "babca" "babcb" "babcc" "bacaa" "bacab" "bacac" "bacba" "bacbb" "bacbc" "bacca" "baccb" "baccc" "bbaaa" "bbaab" "bbaac" "bbaba" "bbabb" "bbabc" "bbaca" "bbacb" "bbacc" "bbbaa" "bbbab" "bbbac" "bbbba" "bbbbb" "bbbbc" "bbbca" "bbbcb" "bbbcc" "bbcaa" "bbcab" "bbcac" "bbcba" "bbcbb" "bbcbc" "bbcca" "bbccb" "bbccc" "bcaaa" "bcaab" "bcaac" "bcaba" "bcabb" "bcabc" "bcaca" "bcacb" "bcacc" "bcbaa" "bcbab" "bcbac" "bcbba" "bcbbb" "bcbbc" "bcbca" "bcbcb" "bcbcc" "bccaa" "bccab" "bccac" "bccba" "bccbb" "bccbc" "bccca" "bcccb" "bcccc" "caaaa" "caaab" "caaac" "caaba" "caabb" "caabc" "caaca" "caacb" "caacc" "cabaa" "cabab" "cabac" "cabba" "cabbb" "cabbc" "cabca" "cabcb" "cabcc" "cacaa" "cacab" "cacac" "cacba" "cacbb" "cacbc" "cacca" "caccb" "caccc" "cbaaa" "cbaab" "cbaac" "cbaba" "cbabb" "cbabc" "cbaca" "cbacb" "cbacc" "cbbaa" "cbbab" "cbbac" "cbbba" "cbbbb" "cbbbc" "cbbca" "cbbcb" "cbbcc" "cbcaa" "cbcab" "cbcac" "cbcba" "cbcbb" "cbcbc" "cbcca" "cbccb" "cbccc" "ccaaa" "ccaab" "ccaac" "ccaba" "ccabb" "ccabc" "ccaca" "ccacb" "ccacc" "ccbaa" "ccbab" "ccbac" "ccbba" "ccbbb" "ccbbc" "ccbca" "ccbcb" "ccbcc" "cccaa" "cccab" "cccac" "cccba" "cccbb" "cccbc" "cccca" "ccccb" "ccccc" "aaaaaa" "aaaaab" "aaaaac" "aaaaba" "aaaabb" "aaaabc" "aaaaca" "aaaacb" "aaaacc" "aaabaa" "aaabab" "aaabac" "aaabba" "aaabbb" "aaabbc" "aaabca" "aaabcb" "aaabcc" "aaacaa" "aaacab" "aaacac" "aaacba" "aaacbb" "aaacbc" "aaacca" "aaaccb" "aaaccc" "aabaaa" "aabaab" "aabaac" "aababa" "aababb" "aababc" "aabaca" "aabacb" "aabacc" "aabbaa" "aabbab" "aabbac" "aabbba" "aabbbb" "aabbbc" "aabbca" "aabbcb" "aabbcc" "aabcaa" "aabcab" "aabcac" "aabcba" "aabcbb" "aabcbc" "aabcca" "aabccb" "aabccc" "aacaaa" "aacaab" "aacaac" "aacaba" "aacabb" "aacabc" "aacaca" "aacacb" "aacacc" "aacbaa" "aacbab" "aacbac" "aacbba" "aacbbb" "aacbbc" "aacbca" "aacbcb" "aacbcc" "aaccaa" "aaccab" "aaccac" "aaccba" "aaccbb" "aaccbc" "aaccca" "aacccb" "aacccc" "abaaaa" "abaaab" "abaaac" "abaaba" "abaabb" "abaabc" "abaaca" "abaacb" "abaacc" "ababaa" "ababab" "ababac" "ababba" "ababbb" "ababbc" "ababca" "ababcb" "ababcc" "abacaa" "abacab" "abacac" "abacba" "abacbb" "abacbc" "abacca" "abaccb" "abaccc" "abbaaa" "abbaab" "abbaac" "abbaba" "abbabb" "abbabc" "abbaca" "abbacb" "abbacc" "abbbaa" "abbbab" "abbbac" "abbbba" "abbbbb" "abbbbc" "abbbca" "abbbcb" "abbbcc" "abbcaa" "abbcab" "abbcac" "abbcba" "abbcbb" "abbcbc" "abbcca" "abbccb" "abbccc" "abcaaa" "abcaab" "abcaac" "abcaba" "abcabb" "abcabc" "abcaca" "abcacb" "abcacc" "abcbaa" "abcbab" "abcbac" "abcbba" "abcbbb" "abcbbc" "abcbca" "abcbcb" "abcbcc" "abccaa" "abccab" "abccac" "abccba" "abccbb" "abccbc" "abccca" "abcccb" "abcccc" "acaaaa" "acaaab" "acaaac" "acaaba" "acaabb" "acaabc" "acaaca" "acaacb" "acaacc" "acabaa" "acabab" "acabac" "acabba" "acabbb" "acabbc" "acabca" "acabcb" "acabcc" "acacaa" "acacab" "acacac" "acacba" "acacbb" "acacbc" "acacca" "acaccb" "acaccc" "acbaaa" "acbaab" "acbaac" "acbaba" "acbabb" "acbabc" "acbaca" "acbacb" "acbacc" "acbbaa" "acbbab" "acbbac" "acbbba" "acbbbb" "acbbbc" "acbbca" "acbbcb" "acbbcc" "acbcaa" "acbcab" "acbcac" "acbcba" "acbcbb" "acbcbc" "acbcca" "acbccb" "acbccc" "accaaa" "accaab" "accaac" "accaba" "accabb" "accabc" "accaca" "accacb" "accacc" "accbaa" "accbab" "accbac" "accbba" "accbbb" "accbbc" "accbca" "accbcb" "accbcc" "acccaa" "acccab" "acccac" "acccba" "acccbb" "acccbc" "acccca" "accccb" "accccc" "baaaaa" "baaaab" "baaaac" "baaaba" "baaabb" "baaabc" "baaaca" "baaacb" "baaacc" "baabaa" "baabab" "baabac" "baabba" "baabbb" "baabbc" "baabca" "baabcb" "baabcc" "baacaa" "baacab" "baacac" "baacba" "baacbb" "baacbc" "baacca" "baaccb" "baaccc" "babaaa" "babaab" "babaac" "bababa" "bababb" "bababc" "babaca" "babacb" "babacc" "babbaa" "babbab" "babbac" "babbba" "babbbb" "babbbc" "babbca" "babbcb" "babbcc" "babcaa" "babcab" "babcac" "babcba" "babcbb" "babcbc" "babcca" "babccb" "babccc" "bacaaa" "bacaab" "bacaac" "bacaba" "bacabb" "bacabc" "bacaca" "bacacb" "bacacc" "bacbaa" "bacbab" "bacbac" "bacbba" "bacbbb" "bacbbc" "bacbca" "bacbcb" "bacbcc" "baccaa" "baccab" "baccac" "baccba" "baccbb" "baccbc" "baccca" "bacccb" "bacccc" "bbaaaa" "bbaaab" "bbaaac" "bbaaba" "bbaabb" "bbaabc" "bbaaca" "bbaacb" "bbaacc" "bbabaa" "bbabab" "bbabac" "bbabba" "bbabbb" "bbabbc" "bbabca" "bbabcb" "bbabcc" "bbacaa" "bbacab" "bbacac" "bbacba" "bbacbb" "bbacbc" "bbacca" "bbaccb" "bbaccc" "bbbaaa" "bbbaab" "bbbaac" "bbbaba" "bbbabb" "bbbabc" "bbbaca" "bbbacb" "bbbacc" "bbbbaa" "bbbbab" "bbbbac" "bbbbba" "bbbbbb" "bbbbbc" "bbbbca" "bbbbcb" "bbbbcc" "bbbcaa" "bbbcab" "bbbcac" "bbbcba" "bbbcbb" "bbbcbc" "bbbcca" "bbbccb" "bbbccc" "bbcaaa" "bbcaab" "bbcaac" "bbcaba" "bbcabb" "bbcabc" "bbcaca" "bbcacb" "bbcacc" "bbcbaa" "bbcbab" "bbcbac" "bbcbba" "bbcbbb" "bbcbbc" "bbcbca" "bbcbcb" "bbcbcc" "bbccaa" "bbccab" "bbccac" "bbccba" "bbccbb" "bbccbc" "bbccca" "bbcccb" "bbcccc" "bcaaaa" "bcaaab" "bcaaac" "bcaaba" "bcaabb" "bcaabc" "bcaaca" "bcaacb" "bcaacc" "bcabaa" "bcabab" "bcabac" "bcabba" "bcabbb" "bcabbc" "bcabca" "bcabcb" "bcabcc" "bcacaa" "bcacab" "bcacac" "bcacba" "bcacbb" "bcacbc" "bcacca" "bcaccb" "bcaccc" "bcbaaa" "bcbaab" "bcbaac" "bcbaba" "bcbabb" "bcbabc" "bcbaca" "bcbacb" "bcbacc" "bcbbaa" "bcbbab" "bcbbac" "bcbbba" "bcbbbb" "bcbbbc" "bcbbca" "bcbbcb" "bcbbcc" "bcbcaa" "bcbcab" "bcbcac" "bcbcba" "bcbcbb" "bcbcbc" "bcbcca" "bcbccb" "bcbccc" "bccaaa" "bccaab" "bccaac" "bccaba" "bccabb" "bccabc" "bccaca" "bccacb" "bccacc" "bccbaa" "bccbab" "bccbac" "bccbba" "bccbbb" "bccbbc" "bccbca" "bccbcb" "bccbcc" "bcccaa" "bcccab" "bcccac" "bcccba" "bcccbb" "bcccbc" "bcccca" "bccccb" "bccccc" "caaaaa" "caaaab" "caaaac" "caaaba" "caaabb" "caaabc" "caaaca" "caaacb" "caaacc" "caabaa" "caabab" "caabac" "caabba" "caabbb" "caabbc" "caabca" "caabcb" "caabcc" "caacaa" "caacab" "caacac" "caacba" "caacbb" "caacbc" "caacca" "caaccb" "caaccc" "cabaaa" "cabaab" "cabaac" "cababa" "cababb" "cababc" "cabaca" "cabacb" "cabacc" "cabbaa" "cabbab" "cabbac" "cabbba" "cabbbb" "cabbbc" "cabbca" "cabbcb" "cabbcc" "cabcaa" "cabcab" "cabcac" "cabcba" "cabcbb" "cabcbc" "cabcca" "cabccb" "cabccc" "cacaaa" "cacaab" "cacaac" "cacaba" "cacabb" "cacabc" "cacaca" "cacacb" "cacacc" "cacbaa" "cacbab" "cacbac" "cacbba" "cacbbb" "cacbbc" "cacbca" "cacbcb" "cacbcc" "caccaa" "caccab" "caccac" "caccba" "caccbb" "caccbc" "caccca" "cacccb" "cacccc" "cbaaaa" "cbaaab" "cbaaac" "cbaaba" "cbaabb" "cbaabc" "cbaaca" "cbaacb" "cbaacc" "cbabaa" "cbabab" "cbabac" "cbabba" "cbabbb" "cbabbc" "cbabca" "cbabcb" "cbabcc" "cbacaa" "cbacab" "cbacac" "cbacba" "cbacbb" "cbacbc" "cbacca" "cbaccb" "cbaccc" "cbbaaa" "cbbaab" "cbbaac" "cbbaba" "cbbabb" "cbbabc" "cbbaca" "cbbacb" "cbbacc" "cbbbaa" "cbbbab" "cbbbac" "cbbbba" "cbbbbb" "cbbbbc" "cbbbca" "cbbbcb" "cbbbcc" "cbbcaa" "cbbcab" "cbbcac" "cbbcba" "cbbcbb" "cbbcbc" "cbbcca" "cbbccb" "cbbccc" "cbcaaa" "cbcaab" "cbcaac" "cbcaba" "cbcabb" "cbcabc" "cbcaca" "cbcacb" "cbcacc" "cbcbaa" "cbcbab" "cbcbac" "cbcbba" "cbcbbb" "cbcbbc" "cbcbca") 
Enter fullscreen mode Exit fullscreen mode

Comments and Feedback

You can find all of these code playbacks in my free 'book', An Animated Introduction to Clojure. I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter @markm208.

Discussion (0)

Forem Open with the Forem app