Download - Kof2008 Itll
![Page 1: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/1.jpg)
Introduction to Lazy Lists2008 UJIHISA, Tatsuhiro
![Page 2: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/2.jpg)
UJIHISA Tatsuhiro• Osaka University
• Ruby, Vim, Haskell and Math
• http://ujihisa.nowa.jp
• http://twitter.com/ujm
• To the US!
![Page 3: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/3.jpg)
Targets
• ★★☆☆☆• Array, Enumerable, IO (File)
Haskell
![Page 4: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/4.jpg)
Lazy lists
![Page 5: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/5.jpg)
• RubyやHaskellを用いて遅延リスト (Lazy list) の概念とその有用性の議論を展開する。説明にはLive codingが含まれる。もしも時間が余れば、全てのプログラマにとって有用である伝統的ツールVimについて簡単な紹介を行う。
![Page 6: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/6.jpg)
Lazy Lists• Lazy lists (or streams)
• List (= Array in Ruby)
• Only needed parts are calculated.
• Haskell supports lazy lists by default.
• Ruby can support it.
![Page 7: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/7.jpg)
List (=Array) in Ruby• Array class
• [0, 1, 2, 3], Array.new(4) {|i| i }
• a #=> [0, 1, 2, 3]a.map {|i| i * 2 } #=> [0, 2, 4, 6]
![Page 8: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/8.jpg)
List• Data structure
• Insert, Delete, Access sequentially
![Page 9: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/9.jpg)
c.f. Array• Data structure
• Insert, Delete, Access randomly, Size
![Page 10: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/10.jpg)
(List again)• Data structure
• Insert, Delete, Access sequentially
![Page 11: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/11.jpg)
Lazy List• Data structure
• Insert, Delete, Access sequentially
![Page 12: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/12.jpg)
Types of lazy lists
• Boundless length lists (長さが無限)
• Undefined length lists (長さが未定義)
• Defined length lists (長さが定義済み)
![Page 13: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/13.jpg)
How to constructlazy lists?
![Page 14: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/14.jpg)
1 class LazyList 2 def initialize(&b) 3 @b = b 4 @i = -1 5 end 6 7 def succ 8 @b[@i+=1] 9 end 10 end
![Page 15: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/15.jpg)
l = LazyList.new {¦i¦ i * 2 } 10.times do p l.succend
![Page 16: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/16.jpg)
def each loop { yield succ }end
l = LazyList.new {¦i¦ i * 2 } l.each do ¦i¦ p i break if i > 10end
![Page 17: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/17.jpg)
![Page 18: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/18.jpg)
Notations• array = 1000+ length array
heavy = lambda { heavy procedure }
• array.take(2).map(&heavy)
• array.map(&heavy).take(2)
![Page 19: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/19.jpg)
File.open(__FILE__) do ¦io¦ a = nil while a = io.gets do p a endend
File.open(__FILE__) do ¦io¦ io.each do ¦a¦ p a endend
![Page 20: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/20.jpg)
File.open(__FILE__) do ¦io¦ io.each do ¦a¦ if a != "\n" p a end endend
File.open(__FILE__) do ¦io¦ io.select {¦a¦ a != "\n" }.each {¦a¦ p a }end
![Page 21: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/21.jpg)
File.open(__FILE__) do ¦io¦ io.each do ¦a¦ if a != "\n" p a end endend
File.open(__FILE__) do ¦io¦ io.select {¦a¦ a != "\n" }.each {¦a¦ p a }end
File.open(__FILE__) do ¦io¦ io. select {¦a¦ a != "\n" }. each {¦a¦ p a }end
![Page 22: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/22.jpg)
Common case• truncation (break)
• filtering (if, unless; select, reject)
• mapping (=, map)
• processing (???, each)folding (<<, inject)
![Page 23: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/23.jpg)
list.each {¦a¦ next if ... b = ... ...}
list. select { ... }. map { ... }. each { ... }
![Page 24: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/24.jpg)
result = 0list.each {¦a¦ next if ... b = ... result += b}
result = list. select { ... }. map { ... }. inject(0) { ... }
![Page 25: Kof2008 Itll](https://reader034.vdocuments.us/reader034/viewer/2022052618/55494074b4c905054d8b501c/html5/thumbnails/25.jpg)
$ sudo gem install lazylist
sq = list { x * x }.where :x => 1..Infinity
# Haskell!# sq = [ x * x ¦ x <- [1..] ]