Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please show me in scheme please. 1. A) Write a function in Scheme to return –1,

ID: 3745859 • Letter: P

Question

Please show me in scheme please.

1.

A) Write a function in Scheme to return –1, 0, +1 depending on two parameters a and b:

a < b return –1

a > b return +1

a = b return 0

b) Write a recursive function in Scheme, called “get-element” which takes two parameters, an index and a list. The function returns the element at the given index position in the list or #f if element is not contained. Indices start at 1 and go to the length of the list.

C) Question: How would you change the function so it returns the whole list if the element is not contained?

Explanation / Answer

A] Function to return –1, 0, +1 depending on two parameters a and b:

(define (compare a b)       ; Name of the function is compare and the parameters are a and b
                            ; I am using cond for this answer. Cond is used to chain multiple possible conditions
    (cond ((< a b) (-1))    ; Checking if a < b
          ((> a b) (1))     ; Checking if a > b
          (else 0)          ; Checking if a = b
    )
)

B] Recursive function which takes two parameters, an index and a list. The function returns the element at the given index position in the list or #f if element is not contained:

(define (get-element index li)
    ; Once again we use the cond statement
    ; The base cases (exit conditions) for the recursion are specified in the first 2 lines of the cond statement
    (cond ((= index 1) (car li))        ; If index = 1, return the first element of the list li
          ((= (cdr li) ('())) (#f))            ; If end of the list is reached and index has not been achieved; index exceeded length of list. Return #f
          (else (get-element (- index 1) (cdr li))      ; Here is the recursive statement; calling get-element again with index-1 and the second half of the list li
    )
)

C] In this case, if you're using recursion, there is no way to obtain the original list. Thus, no modification can be made to the existing code specified in part B]. However, there is a programming paradigm called Dynamic Programming, that you might find useful to solve your problem. I suggest you look into that.

I hope this helps. Please do upvote :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote