Guest User

Untitled

a guest
Dec 9th, 2019
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Skip to content
  3. Pull requests
  4. Issues
  5. Marketplace
  6. Explore
  7. @startrunner
  8.  
  9. 1
  10. 0
  11.  
  12.     0
  13.  
  14. startrunner/fmi-fp-exercises Private
  15. Code
  16. Issues 0
  17. Pull requests 0
  18. Actions
  19. Projects 0
  20. Security
  21. Insights
  22. Settings
  23. fmi-fp-exercises/scheme-homework/01-fun-with-digits.rkt
  24. @startrunner startrunner Scheme Homework 781ce76 11 days ago
  25. 51 lines (42 sloc) 1.79 KB
  26. ; Искаме да дефинираме следните имена: one, two, three, ..., nine, plus, minus, times, div,
  27. ; така че извиквания от типа на (one (plus (three))) (операция с точно две операнди) да връщат легитимни числови стойности (в този случай - 4)
  28. ; Още малко примери:
  29. ; (three (times (five))) -> 15
  30. ; (nine (div (three))) -> 3
  31. ; (eight (minus (four))) -> 4
  32. ;
  33.  
  34.  
  35. #lang racket
  36. (require rackunit rackunit/text-ui)
  37.  
  38. (define (digit-function digit)
  39.   {lambda args
  40.     {cond
  41.       [(empty? args)           digit]
  42.       [(empty? (cdr args))     ((car args) digit)]
  43.       [else                   null]}})
  44.  
  45. (define (operation-function operation)
  46.   {lambda (rightArg) {lambda (leftArg) {operation leftArg rightArg}}})
  47.  
  48. (define zero    (digit-function 0))
  49. (define one     (digit-function 1))
  50. (define two     (digit-function 2))
  51. (define three   (digit-function 3))
  52. (define four    (digit-function 4))
  53. (define five    (digit-function 5))
  54. (define six     (digit-function 6))
  55. (define seven   (digit-function 7))
  56. (define eight   (digit-function 8))
  57. (define nine    (digit-function 9))
  58.  
  59. (define plus    (operation-function +))
  60. (define minus   (operation-function -))
  61. (define times   (operation-function *))
  62. (define div     (operation-function /))
  63.  
  64. ;Test code
  65.  
  66. (define fun-with-digits-tests
  67.   (test-suite "Fun with digits tests"
  68.     (test-case "Digits work as singleton expressions"
  69.                (check-eq? (five) 5))
  70.     ;Division of coprimes is best for testing since we are unlikely to get the right answer with wrong code
  71.     (test-case "Division is chained properly"
  72.                {check-true (=(five (div (six (div (eight (div (three))))))) (/ 5  (/ 6 (/ 8 3))))})
  73.   )
  74. )
  75.  
  76. (run-tests fun-with-digits-tests 'verbose)
  77.  
  78.     © 2019 GitHub, Inc.
  79.     Terms
  80.     Privacy
  81.     Security
  82.     Status
  83.     Help
  84.  
  85.     Contact GitHub
  86.     Pricing
  87.     API
  88.     Training
  89.     Blog
  90.     About
RAW Paste Data