1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h....
-
Upload
liliana-bishop -
Category
Documents
-
view
223 -
download
0
Transcript of 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h....
![Page 1: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/1.jpg)
1
Hamming’s problem
• h is an “Ordered Set”
• 1 ∈ h
• x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h.
• generate all elements of h < limit
![Page 2: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/2.jpg)
2
Let’s solve it
1. Write a test
2. make the test run green
3. clean up the code
➡ remove any duplication
4. repeat until done
![Page 3: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/3.jpg)
3
Regular Expressions in Smalltalk
CS410/510 Advanced Programming Lecture 7:
1
![Page 4: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/4.jpg)
4
Just Like HaskellOne subclass for each
alternative representation
![Page 5: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/5.jpg)
5
Write Tests
1. Run tests
2. get message not understood
3. define method
4. repeat from 1
…
19. get real failure
![Page 6: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/6.jpg)
6
What’s the problem?
![Page 7: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/7.jpg)
7
I need an instance, not the class
• But there need be only one instance of REEmpty
• Enter: the Singleton pattern.
• make a class instance-variable called uniqueInstance
• make a class-side method named default
• override new to be an error
![Page 8: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/8.jpg)
8
What do we have so far?
![Page 9: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/9.jpg)
9
Convenience Operations
•Write tests:
self assert: $a asRE printString = 'a'
self assert: (a + b) printString = 'a+b'
•Why compare printStrings?
![Page 10: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/10.jpg)
10
Where do the operation methods go?
• In the abstract superclass RegularExpression
- so that they work for all the subclasses
![Page 11: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/11.jpg)
11
Refactor tests to remove duplication
assert: anExpression printsAs: aprintString self assert: anExpression printString = aprintString
testPrinting self assert: epsilon printsAs: '#'. self assert: a printsAs: 'a'. self assert: b printsAs: 'b'. self assert: aORb printsAs: 'a+b'. self assert: ab printsAs: 'ab'. self assert: abStar printsAs: 'ab*'.
![Page 13: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/13.jpg)
13
meaning1: sets of strings
• Code very similar to Tim’s Haskell version
• Only tricky part is star
• Haskell version:
![Page 14: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/14.jpg)
14
Smalltalk
• Complicated enough to need a helper method
• Is there a simpler way to calculate * ?
REStar
RegularExpression
![Page 15: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/15.jpg)
15
Cross tests
• introspect on the instance variables of the test case
• select those that respond to the meaning1 message
• check that for every string str in re meaning1
•re meaning2: str is true
![Page 16: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/16.jpg)
16
Now RE’s pass the tests
![Page 17: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/17.jpg)
17
Finite State Machines
![Page 18: 1 Hamming’s problem h is an “Ordered Set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h. generate all elements of h < limit.](https://reader036.fdocuments.in/reader036/viewer/2022062309/56649ebd5503460f94bc6d49/html5/thumbnails/18.jpg)
18
The code with NFSM