1 / 18

Hamming’s problem

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. Let’s solve it. Write a test make the test run green clean up the code remove any duplication repeat until done. Regular Expressions in Smalltalk.

mimi
Download Presentation

Hamming’s problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • Write a test • make the test run green • clean up the code • remove any duplication • repeat until done

  3. Regular Expressions in Smalltalk • CS410/510 Advanced Programming • Lecture 7: 1

  4. One subclass for each alternative representation Just Like Haskell

  5. Write Tests 1. Run tests 2. get message not understood 3. define method 4. repeat from 1 … 19. get real failure

  6. What’s the problem?

  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

  8. What do we have so far?

  9. Convenience Operations • Write tests: self assert: $a asRE printString = 'a' self assert: (a + b) printString = 'a+b' • Why compare printStrings?

  10. Where do the operation methods go? • In the abstract superclass RegularExpression • so that they work for all the subclasses

  11. Refactor tests to remove duplication 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*'. assert: anExpression printsAs: aprintString self assert: anExpression printString = aprintString

  12. which brings us to…

  13. meaning1: sets of strings • Code very similar to Tim’s Haskell version • Only tricky part is star • Haskell version:

  14. Smalltalk REStar • Complicated enough to need a helper method • Is there a simpler way to calculate * ? RegularExpression

  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

  16. Now RE’s pass the tests

  17. Finite State Machines

  18. The code with NFSM

More Related