Guest User

Untitled

a guest
Dec 16th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. (require-extension test) ; chicken-install -s test
  2.  
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4. ;; Expr -> Bool
  5. ;; Takes an expression and produce #t if it is an atom; #f otherwise.
  6. ;;
  7. ;; NOTE:
  8. ;; Chicken has its own implementation of atom?. It produces
  9. ;; `#t` for '(). This version (from the book) produces #f for '().
  10.  
  11. (test-group "`atom?':"
  12. (test "is 'yoda an atom? Yes, it is."
  13. #t
  14. (atom? 'yoda))
  15. (test "'(foo) should not be an atom"
  16. #f
  17. (atom? '(foo))))
  18.  
  19. (define atom?
  20. (lambda (x)
  21. (and (not (pair? x)) (not (null? x)))))
  22.  
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;; (list-of Atom) -> Bool
  25. ;; Produce #t if l is a list of atoms, #f otherwise.
  26.  
  27. (test-group "`lat?'"
  28. (test "list with only atoms should be #t"
  29. #t
  30. (lat? '(a b c)))
  31. (test "list with lists inside should not be #t"
  32. #f
  33. (lat? '(a (b) c))))
  34.  
  35. (define lat?
  36. (lambda (l)
  37. (cond
  38. ((null? l) #t)
  39. ((atom? (car l)) (lat? (cdr l)))
  40. (else #f))))
  41.  
  42.  
  43. ;; Atom (list-of Atom) -> Bool
  44. ;; Produce #t if the atom a exists in lat.
  45. (define member?
  46. (lambda (a lat)
  47. (cond
  48. ((null? lat) #f)
  49. (else
  50.  
  51. (or (eq? (car lat) a)
  52. (member? a (cdr lat)))))))
  53.  
  54.  
  55. ;; Atom (list-of Atom) -> (list-of Atom)
  56. ;; Produce list of atoms with first occurrence of `a' removed.
  57. ;; If `a' doesn't exist in `lat', produce the unmodified list.
  58. (define rember
  59. (lambda (a lat)
  60. (cond
  61. ((null? lat) '())
  62. ((eq? (car lat) a) (cdr lat))
  63. (else
  64. (cons (car lat) (rember a (cdr lat)))))))
  65.  
  66.  
  67. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  68. ;; List -> List
  69. ;; Produce list with first element in each sub-list.
  70. ;; ASSUME: input list can be empty or contain only
  71. ;; non-empty lists.
  72. (test-group "`firsts':"
  73. (test "should produce '()"
  74. '()
  75. (firsts '()))
  76.  
  77. (test "should get firsts"
  78. '(a c y k)
  79. (firsts '((a b) (c) (y z) (k t x)))))
  80.  
  81. (define firsts
  82. (lambda (l)
  83. (cond
  84. ((null? l) '())
  85. (else
  86. (cons (car (car l))
  87. (firsts (cdr l)))))))
Add Comment
Please, Sign In to add comment