1 hamming’s problem h is an “ordered set” 1 ∈ h x ∈ h ⇒ 2*x ∈ h, 3*x ∈ h, 5*x ∈ h....

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

Upload: liliana-bishop

Post on 13-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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 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

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

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

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

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

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

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 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

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

18

The code with NFSM