Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Skip to content
- Pull requests
- Issues
- Marketplace
- Explore
- @startrunner
- 1
- 0
- 0
- startrunner/fmi-fp-exercises Private
- Code
- Issues 0
- Pull requests 0
- Actions
- Projects 0
- Security
- Insights
- Settings
- fmi-fp-exercises/scheme-homework/01-fun-with-digits.rkt
- @startrunner startrunner Scheme Homework 781ce76 11 days ago
- 51 lines (42 sloc) 1.79 KB
- ; Искаме да дефинираме следните имена: one, two, three, ..., nine, plus, minus, times, div,
- ; така че извиквания от типа на (one (plus (three))) (операция с точно две операнди) да връщат легитимни числови стойности (в този случай - 4)
- ; Още малко примери:
- ; (three (times (five))) -> 15
- ; (nine (div (three))) -> 3
- ; (eight (minus (four))) -> 4
- ;
- #lang racket
- (require rackunit rackunit/text-ui)
- (define (digit-function digit)
- {lambda args
- {cond
- [(empty? args) digit]
- [(empty? (cdr args)) ((car args) digit)]
- [else null]}})
- (define (operation-function operation)
- {lambda (rightArg) {lambda (leftArg) {operation leftArg rightArg}}})
- (define zero (digit-function 0))
- (define one (digit-function 1))
- (define two (digit-function 2))
- (define three (digit-function 3))
- (define four (digit-function 4))
- (define five (digit-function 5))
- (define six (digit-function 6))
- (define seven (digit-function 7))
- (define eight (digit-function 8))
- (define nine (digit-function 9))
- (define plus (operation-function +))
- (define minus (operation-function -))
- (define times (operation-function *))
- (define div (operation-function /))
- ;Test code
- (define fun-with-digits-tests
- (test-suite "Fun with digits tests"
- (test-case "Digits work as singleton expressions"
- (check-eq? (five) 5))
- ;Division of coprimes is best for testing since we are unlikely to get the right answer with wrong code
- (test-case "Division is chained properly"
- {check-true (=(five (div (six (div (eight (div (three))))))) (/ 5 (/ 6 (/ 8 3))))})
- )
- )
- (run-tests fun-with-digits-tests 'verbose)
- © 2019 GitHub, Inc.
- Terms
- Privacy
- Security
- Status
- Help
- Contact GitHub
- Pricing
- API
- Training
- Blog
- About
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement