1
votes

I am writing a program which takes in a list and outputs true if the elements in the list alternate signs. For example if the first number is positive, the second number must be negative and then the third number must be positive again and so on.

I've tried implementing a simple cond statement (shown in the code) but keep coming across an error in my check-expect. The error states: first: expects only 1 argument, but found 2.

(define (alternating? lst)
(cond [(empty? lst) true]
[(> (first lst) 0)
 (cond [(< (first (rest lst) 0)) (alternating? (rest lst))])]
[(< (first lst) 0)
 (cond [(> (first (rest lst) 0)) (alternating? (rest lst))])]
[else false]))

When looking at the code, it looks like first does in fact only take in one argument from the list, but the error says that this is not the case.

1

1 Answers

2
votes

On lines 3 and 5, you are correctly calling first with one argument, namely lst:

(first lst)

On lines 4 and 6, you are calling first with two arguments, namely (rest lst) and 0:

(first (rest lst) 0)

I think what you want is this:

(< (first (rest lst)) 0)
;                   ↑   ↑

instead of this:

(< (first (rest lst) 0))
;                   ↑  ↑