Reputation: 83
The bottom code is to remove adjacent duplicate elements from a sorted list L.
(define a
(lambda (L)
(cond
((null? L) L)
((null? (cdr L)) L)
((eqv? (car L) (car (cdr L))) (a (cdr L)))
(else (cons (car L) (a (cdr L)))))))
Question is "Write a similar function that uses the imperative features of Scheme to modify L 'in place,' rather than building a new list. Compare that to the code above in terms of brevity, conceptual clarity, and speed.
But I don't understand clearly what imperative features. Plz help.
Upvotes: 2
Views: 1994
Reputation: 236122
The question asks for you to use some or all of the following procedures to write the solution: set-car!
, set-cdr!
, set!
, etc. In this context, the imperative features refer to those Scheme procedures that mutate data - using them you can modify a list structure "in place", meaning: without creating a new list, instead directly modifying the list passed as parameter.
Notice that all the procedures' names mentioned end with a !
(pronounced: bang!), this is to warn the programmer that they will change the object received as parameter. Contrast this with the procedures you've been using up until now, that won't change the object and if necessary will return a new object with the requested modification - a functional programming style, unlike the imperative programming style that will happen when you start using bang!-procedures.
Take a look at section 3.3 Modeling with Mutable Data in the SICP book, it will explain in detail how to deal with operations that mutate data.
Upvotes: 1