01-11-Exercises.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (import
  2. (except (rnrs base) let-values map error)
  3. (only (guile)
  4. lambda* λ
  5. current-output-port)
  6. (srfi srfi-1))
  7. (define (mappend fn the-list)
  8. (apply append (map fn the-list)))
  9. ;; ============
  10. ;; Exercise 1.1
  11. ;; ============
  12. ;; Define a version of last-name that handles "Rex Morgan MD", "Morton Downey, Jr.," etc.
  13. (define (first-name name titles)
  14. ;; assume that there is at least one first name
  15. (cond
  16. [(null? name) '()]
  17. [(member (car name) titles)
  18. (first-name (cdr name) titles)]
  19. [else (car name)]))
  20. (define (remove-titles name titles)
  21. ;; not using let, because it has not been introduced yet
  22. (cond
  23. [(null? name) '()]
  24. [(member (car name) titles)
  25. (remove-titles (cdr name) titles)]
  26. [else
  27. (cons (car name)
  28. (remove-titles (cdr name) titles))]))
  29. (define (last-name name titles)
  30. "idea: first remove all titles, then remove the first name and what remains shall be the last name"
  31. (last (remove-titles name titles)))
  32. (last-name '(Madam Major General Paula Jones)
  33. '(Mr Mrs Miss Ms Sir Madam Dr Admiral Major General Jr MD))
  34. ;; Notes:
  35. ;; For more just add to the list of titles.
  36. ;; However if there are names which are also titles, this system breaks badly.
  37. ;; ============
  38. ;; Exercise 1.2
  39. ;; ============
  40. ;; Write a function to exponentiate, or raise a number to an integer power.
  41. ;; For example: (power 3 2) = 3^2 = 9.
  42. (define (exponentiate acc base exponent)
  43. (cond [(= exponent 0) acc]
  44. [else (exponentiate (* acc base)
  45. base
  46. (- exponent 1))]))
  47. ;; ============
  48. ;; Exercise 1.3
  49. ;; ============
  50. ;; Write an function that counts the number of atoms in an expression.
  51. ;: For example: (count-atoms '(a (b) c)) = 3.
  52. ;; Note that there is something of an ambiguity in this:
  53. ;; Should (a nil c) count as 3 atoms, or as 2, because ot is equivalent to (a () c)?
  54. (define (atom? sth)
  55. (and (not (pair? sth))
  56. (not (null? sth))))
  57. (define (count-atoms expr)
  58. (cond [(null? expr) 0] ; or return 1 here for counting ()
  59. [(atom? expr) 1]
  60. [else (+ (count-atoms (car expr))
  61. (count-atoms (cdr expr)))]))
  62. ;; ============
  63. ;; Exercise 1.4
  64. ;; ============
  65. ;; Write a function that counts the times an expression occurs anywhere within another expression.
  66. ;; For example: (count-anywhere 'a '(a ((a) b) a)) => 3
  67. (define (count-anywhere seek-expr sth)
  68. (cond [(null? sth) 0]
  69. [(atom? sth)
  70. (if (eq? seek-expr sth) 1 0)]
  71. [(eq? seek-expr (car sth))
  72. (+ 1 (count-anywhere seek-expr (cdr sth)))]
  73. [else (+ (count-anywhere seek-expr (car sth))
  74. (count-anywhere seek-expr (cdr sth)))]))
  75. ;; ============
  76. ;; Exercise 1.5
  77. ;; ============
  78. ;; Write a function to computer the dot product of 2 sequences of numbers, represented as lists.
  79. ;; The dot product is computed by multiplying the corresponding elements and then adding up the resulting products.
  80. ;; For example: (dot-product '(10 20) '(3 4)) = 10 * 3 + 20 * 4 = 110
  81. (define (dot-product l1 l2)
  82. (apply + (map * l1 l2)))