Is there a limit to how many car and cdr we can chain in Scheme? - scheme

Some answers on here just say that you can chain multiple car and cdr to reduce the clutter they create so, you could use (cadr (list)) instead of (car (cdr (list) )), but I have tried chaining multiple car and cdr and sometimes it will stop working on like the 5th one or something, I tried googling if there is a limit or something but couldnt find any answers.
specifically my problem shows here -> trying to get 7 from a list only using car and cdr.
list is (1(2(3(4(5(6(7)))))))))
(display (car(car(cdr(car(cdr(car(cdr(car(cdr(car (cdr (car (cdr '(1(2(3(4(5(6(7)))))))))))))))))))))
-> shows 7
(display (caadadadadadadr '(1(2(3(4(5(6(7)))))))))
-> shows error
Is this not how this works?

You aren't specific about which Scheme you're using. But for every Scheme implementation I'm aware of, there's a limit to how many car/cdr compositions are provided. In R5RS, for example, we see
library procedure: cddddr pair
These procedures are compositions of
car and cdr, where for example caddr could be defined by
(define caddr (lambda (x) (car (cdr (cdr x))))).
Arbitrary compositions, up to four deep, are provided. There are
twenty-eight of these procedures in all.

Related

Filtering a list with indexes MIT-scheme

Is there a good way to filter a list using each element and its index in scheme? This is how I'm doing it now, but it seems overly complex
(map cdr
(filter (lambda (index-and-element)
(filter-proc (car index-and-element)
(cdr index-and-element)))
(map cons (iota (length l))
l)))
Looks perfectly fine to me. Except, perhaps you meant map cdr...
Personally I like very short variable names whenever possible, so instead of index-and-element I'd just use ie -- they are unimportant, it's just some wiring, so make them as invisible as possible.
Another possibility is to use (map list ...) initially, not (map cons ...), and then inside the lambda to use (apply filter-proc ie). this way filter-proc is called with two arguments, so it can be defined (define (filter-proc idx elt) ...).
And after all that, since it is indeed complex and error-prone to type all this anew each time, we'd just define a higher-order function for this so it is easier to use, like
(define (indexed-filter ipred lst)
(filter (lambda (ie)
(apply ipred ie))
(map list (iota (length lst))
lst)))
;; (define (ipred idx elt) ....) ; returns Bool
and use it whenever the need arises.
I've intentionally left out the (map cadr ...) post-processing step, since you could sometimes want to get the indices of the matching elements instead of the elements themselves. So that would be part of the calling "protocol" so to speak -- whether you're interested in indices, elements, or both, you'd just tweak the call to this general procedure.

scheme : What's imperative features?

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.
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.

Scheme program to turn every element of a list into atoms

I need a program that takes a list as input and turns every element found in the list into atoms. Here's what i have so far but i keep running into errors.
(define make-lat
(lambda (l)
(cond
((null? l) (quote ()))
(else
(cond
((list? (car l))
(cons (caar l)
make-lat (cdr l)))
(else
((atom? (car l))
(cons (car l)
(make-lat(cdr l)
)))))))))
Can someone help me out?
Your code looks a little disorganized to me, and I think you might want to think about following the steps in the How To Design Programs Design Recipe:
Step one: can you write a purpose statement for your program? It should say what the function does.
Step two: can you write a contract? It should say what kind of data the program takes in, and
what it produces. You've got to be specific, here, and any kind of data that you specify must either be built-in or have an explicit "data definition".
Step three: write some test cases! Provide a sample input, and the output that you expect.
For more design recipe goodness, check out How To Design Programs.
Your question looks like homework.
Here's a couple of things you could do to try and debug.
Use the Repl to try out parts of the function. E.g if in a cond, you are checking for null, you could do it on the REPL too.
(null? '(1 2 3)) or (null? '())
Check individual S-Exps. Are you sure that every function that you call has an opening and closing parens.
As a tip, simplify your cond's. You don't need to nest conds. You can put all conditions one by one.

Scheme - turn each element of a list into a list (list of lists)

I have list in scheme: (3 4 2) and I want to make it a list of lists, i.e: ((3) (4) (2)), How can I do it?
Thank you.
You can utilize the map function, e.g. (map list '(3 4 5)), which is the bread and butter of all list manipulation. It's equivalent to a list comprehension in python, or a for loop in java.
for the record:
You can also use reduce and filter for more complicate list-manipulation tasks. You do not really need anything else, besides maybe apply.
If you wanted to do it Little Schemer style with only basic functions you could create something like this:
(define make-list
..(lambda (lat)
....(cond
......((null? lat) (quote()))
......(else (cons (cons (car lat) (quote()))
..................(make-list (cdr lat))))))))

Removing all duplicate members from a list in scheme

I am trying to remove duplicates in a list, using recursion. This is what I have. It only removes the first duplicate, not all of them.
My idea is to look at the first member, check if its a member of the rest of the list, if so, call the function again. If not, create a list with the first member and the result from calling the function again. I don't understand why it doesn't remove all the duplicates.
(define (removeDupes L)
(cond ((null? L) ())
((list? (member (car L) (cdr L))) removeDupes (cdr L))
(#T (cons ((car L) (removeDupes (cdr L)))))))
This is what I modified it to, and it works!! And I understand what was wrong with the cons. It needs two parameters and I only gave it one. I still have no Idea why the third line did not work....
(define (removeDupes L)
(cond ((null? L) ())
((list? (member (car L) (cdr L)))(removeDupes(cdr L)))
(#T (cons (car L) (removeDupes (cdr L))))))
There are multiple errors in your code, but the one that's probably causing the problem you've reported here is that your parentheses are wrong in the third line. You're trying to call removeDupes but your code doesn't actually do so; instead the value in that case ends up being (cdr L). Can you see why?
When you fix this, you'll find that your code starts producing errors. For the one you're likely to encounter first: take a careful look at how you're invoking cons on the last line. For the one you're likely to encounter next: remember that () is not self-evaluating in Scheme.
(I think this sort of thing is much harder to miss if you take care with the spacing and layout of your code. Put spaces between the elements of every list, for instance. Until you get so familiar with this stuff that these mistakes stop happening, you might want to make a habit of checking the parentheses any time you run across a mysterious error: have you missed a ( at the start of an expression, or put an extra ( before the arguments of a function, or forgotten the extra level of parens round a cond clause, or etc. etc. etc. Don't worry: after a while it'll stop happening...)

Resources