PasteRack.org
Paste # 3328
2025-02-15 04:29:17

Fork as a new paste.

Paste viewed 183 times.


Embed:

  1. #lang scheme
  2. ;;-------------------------------------------------------------------------------
  3. ;; Name:
  4. ;; Pledge:
  5. ;;-------------------------------------------------------------------------------
  6. ;; This lab serves as an introduction to using recursion in Scheme.
  7. ;; You should implement every function in this program recursively.
  8. ;;
  9. ;; NOTE: the empty list is a valid input for each function!
  10. ;;
  11. ;; Also, just because your code works for provided test cases
  12. ;;   doesn't mean it always works.
  13. ;; Always come up with your own test cases too!
  14. ;; Implement your version of multiplication denoted by the symbol @. @ should take as arguments
  15. ;; an integer a and a non-negative integer b. The return value of @ should be a*b. HOWEVER, the only
  16. ;; arithmetic operation you're allowed to use is +.
  17. ;; Examples:
  18. ;; (@ 7 3) -> 21
  19. ;; (@ 8 0) -> 0
  20. ;; (@ -3 4) -> 12
  21. ;; (@ 0 4) -> 0
  22. ;; (@ -5 1) -> -5
  23. ;; Type signature: (@integer integer) -> integer
  24. (define (@ a b)
  25.    "Your code comes here"
  26.   )
  27. ;; Implement the "power" function which takes as its arguments a positive integer x and a non-negative
  28. ;; integer n. "power" shoud return x raised to the power of n. HOWEVER, the only
  29. ;; arithmetic operation you're allowed to use is *.
  30. ;; Examples:
  31. ;; (power 3 2) -> 9
  32. ;; (power 2 10) -> 1024
  33. ;; (power 3 1) -> 3
  34. ;; (power 7 0) -> 1
  35. ;; Type signature: (power integer integer) -> integer
  36. (define (power x n)
  37.  "Your code comes here"
  38.   )
  39. ;; Implement "nth", which returns the element at index n of the provided list.
  40. ;; The first element of the list is at index 0, the second at index 1, etc.
  41. ;; You may assume that the "n" provided is non-negative and less than the list's length.
  42. ;; Examples:
  43. ;; (nth 1 '(Sandeep Owen Jared Sarvani)) -> Owen
  44. ;; (nth 5 '("zero" "one" "two" "three" "four" "five")) -> "five"
  45. ;; (nth 0 '(a b c)) -> 'a
  46. ;; Type signature: (nth integer list) -> element from list
  47. (define (nth n lst)
  48.  "Your code comes here"
  49.   )
  50. ;; Implement "sum of squares", which returns the summation of squares of all the elements in
  51. ;; the given list. You may assume that the list will only contain numbers.
  52. ;; Note: mathematical convention is that the sum of squares of nothing is 0.
  53. ;; Examples:
  54. ;; (sum2 '(1 3 5 1)) -> 36
  55. ;; (sum2 '(75050 344 0 -70 125)) -> 5632641361
  56. ;; Type signature: (sum2 number-list) -> number
  57. (define (sum2 lst)
  58.  "Your code comes here"
  59. )
  60. ;; Implement "product", which returns the result of multiplying all elements in the list.
  61. ;; You may assume that the list will only contain numbers.
  62. ;; Note: mathematical convention is that the product of nothing is 1.
  63. ;; Examples:
  64. ;; (product (list 1 3 5 4)) -> 60
  65. ;; (product '(100 -50 6789 4183457)) -> -142007447865000
  66. ;; Type signature: (product number-list) -> number
  67. (define (product lst)
  68.  "Your code comes here"
  69. )
  70. ;; Implement the following continued fraction function denoted as "cf" and defined as follows.
  71. ;; "cf" takes as its only argument a list of positive integers. If the list has length 1, then the
  72. ;; only element of the list is returned as the function's value. Otherwise, the function returns the
  73. ;; fraction built according to the pattern below
  74. ;; (cf '(a))     ->  a
  75. ;;
  76. ;; (cf '(a b))   ->        1
  77. ;;                    a + ---
  78. ;;                         b
  79. ;;
  80. ;; (cf '(a b c)) ->               1
  81. ;;                        ------------
  82. ;;                                    1
  83. ;;                              b   + ---
  84. ;;                                     c
  85. ;;
  86. ;; (cf '(a b c d)) ->               1
  87. ;;                         ----------------
  88. ;;                                        1
  89. ;;                              b   + ---------
  90. ;;                                       1
  91. ;;                                          ---
  92. ;;                                           d
  93. ;;
  94. ;; and so on.
  95. ;;
  96. ;; Examples:
  97. ;; Approximation for Pi
  98. ;; (exact->inexact (cf '(3 7 15 1 292 1))) -> 3.141592653921421
  99. ;; Approximation for square root of 2
  100. ;; (exact->inexact (cf '(1 2 2 2 2 2 2 2 2 2 2 2 2 2 2))) -> 1.4142135623637995
  101. ;; Approximation for golden ratio
  102. ;; (exact->inexact (cf '(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1))) -> 1.618034447821682
  103. ;;
  104. ;; Type signature (cf number-list) -> fraction
  105. (define (cf number-list)
  106.  "Your code comes here"
  107.   )
  108. ;; Implement "2filter", which accepts a predicate (a function that returns a boolean) and a list,
  109. ;; and returns the same list except that for every element satisfing the predicate a new copy of
  110. ;; that element is added. The new copy should be added beside the element which makes the predicate
  111. ;; return #t.
  112. ;; Examples
  113. ;; (2filter zero? '(1 0 2 34 56 1 0)) -> '(1 0 0 2 34 56 1 0 0)
  114. ;; (2filter even? '(0 1 2 3 4 5 6 7 8 9)) -> '(0 0 1 2 2 3 4 4 5 6 6 7 8 8 9)
  115. ;; (2filter number? '(shave and 1 haircut 2 bits)) -> '(shave and 1 1 haircut 2 2 bits)
  116. ;; (2filter (lambda (x) (> x 10)) '(1 0 2 34 56 1 0) ) -> (1 0 2 34 34 56 56 1 0)
  117. ;; Type signature: (2filter predicate list) -> list
  118. (define (2filter pred lst)
  119.  "Your code comes here"
  120.  )
  121. ;; Created January 2018 by Samuel Kraus and Edward Minnix
  122. ;; Updated January 2020 by Jared Pincus
  123. ;; Modified Januare 2022 by Jacek Ossowski

=>