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

Scheme Write a procedure in Scheme called charflip that takes a string as a para

ID: 3673972 • Letter: S

Question

Scheme

Write a procedure in Scheme called charflip that takes a string as a parameter and returns a copy of this string, flipping the case of every character in an odd position (index 1, 3, 5, etc..).

It is suggested that you do this recursively. This means cons, car and cdr will come in handy, as will these functions:

• char-upper-case? • char-downcase

• char-upcase

• list->string

• string->list

The function must be able to be called like so:

scheme@(guile-user)> (charflip "hatburg")

$16 = "hAtBuRg"

Explanation / Answer

Good Wishes,

Firts let me give you the code

PROCEDURE:

(define (charflip strings)
(if (null? strings) '()
(cons (if (0! (remainder (get-list-index strings (car strings)) 2))
(if (char-lower-case? car strings) (list->string (map char-upcase (string->list strings))))
(if (char-upper-case? car strings) (list->string (map char-downcase (string->list strings)))))
(cdr strings))))


(define (get-list-index l el)
(if (null? l)
-1
(if (= (car l) el)
0
(let ((result (get-list-index (cdr l) el)))
(if (= result -1)
-1
(1+ result))))))

EXPLAINATION:

Firstly i defined charflip procedure which would first access the first element in the string usind car

(car strings)

and then check its index using get-list-index procedure which would return the index of the character in the string .

(get-list-index strings (car strings))

if this index is a odd number i.e. when divided by 2 remainder is not zero,

(if (0! (remainder (get-list-index strings (car strings)) 2))

then i would check the case of the character and would flip it,

(if (char-lower-case? car strings) (list->string (map char-upcase (string->list strings))))
(if (char-upper-case? car strings) (list->string (map char-downcase (string->list strings)))))

now i would concatenate it to the remaining string using cons and cdr, I recursively called the procedure and hence the output.

(cons (if (0! (remainder (get-list-index strings (car strings)) 2))
(if (char-lower-case? car strings) (list->string (map char-upcase (string->list strings))))
(if (char-upper-case? car strings) (list->string (map char-downcase (string->list strings)))))
(cdr strings))))

Hope this is clear.

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