1

I am having trouble writing a Scheme procedure that takes a list and returns the list created by switching successive elements in the list.

For example, if I were to call: (newlist ‘((a b) (c d) e f g)) I would get: ‘( (b a) (d c) f e g)

I have tried the following so far, but I am getting an error when trying to compile:

(define (newlist givenList)
  (cond ; Condition statement
    ((null? givenList)
        '())
        ((null? (cdr givenList))
            (list (car givenList)))
        (list (cadr givenList) (car givenList)))
    (newlist (cddr givenList)))

cddr: contract violation expected: (cons/c any/c pair?) given: (g)

This language is very different from the ones I've previously worked with, so I'm admittedly a little stumped.

Will Ness
  • 70,110
  • 9
  • 98
  • 181

1 Answers1

2

Your parenthesization is wrong. Here's what you wrote:

(define (newlist givenList)
  (cond ; Condition statement
      ( (null? givenList)
          '()
          )
      ( (null? (cdr givenList))
          (list (car givenList))
          )
      ( list                        ; NB
          (cadr givenList)
          (car givenList)
          ) )
  (newlist                          ; NB
     (cddr givenList)))

Judging by the error message, you use Racket. Clicking on an open bracket in its editor will highlight the expression to its ending bracket. Pressing "Alt-right arrow" will then make your cursor point jump to the closing bracket, and then pressing "Alt-left arrow", back to the opening one. This way you can see what's what, and reformat and add whitespace to make the code clearer for you.

In particular, your cond's third clause starts with list. It is tested for being false or true, and since it's not #f it is considered to be true. So the following two expressions will be evaluated, while the value of the first will be ignored.

Then, as we can now see, the value of the whole cond expression is ignored, and another form, (newlist ...) is evaluated -- unconditionally.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • when you correct the parenthesization, please post a new question with the updated code. then the logic of your program could be addressed. – Will Ness Sep 18 '21 at 16:37
  • "_Judging by the error message, you use Racket._" -- OP also used the Racketism `null` to represent the empty list in [another recent question](https://stackoverflow.com/q/69230261/6879826); I've added the [tag:racket] tag to these questions. – ad absurdum Sep 19 '21 at 14:59
  • @adabsurdum you forgot this one. :) (I'll do it shortly) --- on an unrelated note, have you noticed tfb had left SO... bummer. – Will Ness Sep 19 '21 at 19:31
  • 1
    Hrmm.... I don't know how I missed adding the tag to this question ;) On the unrelated note, I did notice that tfb left. That is too bad; they posted a lot of great answers. I have occasionally seen tfb on the #commonlisp irc channel recently. IAC, the common lisp and scheme tags have not seemed very active of late, so they probably aren't missing too much ;) – ad absurdum Sep 20 '21 at 01:19