1 Replies - 779 Views - Last Post: 21 March 2015 - 11:29 PM

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Scheme: how to use apply on each car of a list?

Posted 15 March 2015 - 06:04 PM

I am trying to multiply the car of each element in a list by -1.
So for example, if x = ((2 9)(-4 6)(8 0)) I want the new x to be something like ((-2 9)(4 6)(-8 0))

This is what I tried:
(define changeSign
  (lambda (p2 newP2)
    ;if the list is empty, return itself. if it is not empty, return the additive inverse
    ;of the cars car, along with the additive inverse of its cdrs car 
    (if (not(null? (car p2))) (changeSign((cdr p2) (-1 * (car (car p2))))) p2)
    )
  )



However I am getting an error stating:
write method in ellipsis-snip%: arity mismatch;
 the expected number of arguments does not match the given number
  expected: 1
  given: 2
  arguments...:
pointing at (changeSign((cdr p2) (-1 * (car (car p2))))).

Why is it saying that my changeSign needs one argument instead of two?

I also tried doing something like
(if (not(null? (car p2))) (apply -1 * (car p2)) p2)
but that doesnt look right to me either.

Is This A Good Question/Topic? 0
  • +

Replies To: Scheme: how to use apply on each car of a list?

#2 GWatt  Icon User is online

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: Scheme: how to use apply on each car of a list?

Posted 21 March 2015 - 11:29 PM

Remember that scheme has prefix functions and operators, not infix.
The call to (-1 * (car (car p2))) should be (* -1 (car (car p2))) though It could be written more simply as (- (caar p2))
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1