Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Exercises after The Little Schemer chapter 2 (2)
;; in the previous solution we implemented 2nd and last
;; but we did not define proper return values for the edge cases,
;; this time we will
;; your task will be to provide those values, as you can imagine
;; (sum 4 'gatto) does not have an answer
;; make it so that expressions will return true
;; a suggestion would be to paste the definitions of 2nd and last
;; above the exercises and fix the errors you get, by returning proper values
;; this is the implementation of the sum function used below
(define sum
(lambda (n m)
(+ n m)))
(eq? 6 (sum (second '(2 4)) (last '(2)))) ;; this one already returns true
(eq? 2 (sum (second '(1 2 3)) (last '())))
(eq? (second '()) (last '()))
;; your second and last exercise is to implement the nth function
;; which given a list and a number returns the item in the list at that position
;; for example
(nth '(1 2 3) 0) ;; 1
(nth '(1 2 3) 1) ;; 2
;; assume that you will be given a lat, provide meaningful return values for edge cases
(define nth
(lambda (lat position)
;; ???
;; you will need to
;; ask yourself questions about the input
;; take actions based on the answers you get
;; recursively check if the current atom is the one at the position you want
;; hint: use the function "-" to move to the correct position,
;; and (eq? position 0) to understand when you're at the correct position
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment