Reputation: 13
I am having trouble writing a function in racket that is supposed to take another function as it's first parameter and a list as its second.
All seems to go well on the initial call to the function but when I try to recursively call it with the function as a parameter it is wanting parameters to the function. I want this parameter function to be passed as a function upon the recursion but used for evaluation other places in the definition. Any help would be greatly appreciated.
(define apply-to-pairs
(lambda (arg1 arg2)
(if (or (equal? (length arg2) 0)
(equal? (length arg2) 1))
'()
(cons (arg1 (car arg2) (car (cdr arg2)))
(apply-to-pairs (arg1 (cdr (cdr arg2))))))))
This is my code and it compiles fine but when the recursive call to apply-to-pairs is performed the program wants parameters for arg1 while I just want it to be passed as the function definition it comes in as. I get there error 'expected 2 parameters but only found 1' because it is looking at (cdr (cdr arg2))
as an argument to it while i mean that to be the second parameter to the recursive call.
Upvotes: 1
Views: 232
Reputation: 737
In your recursive call to apply-to-pairs you are calling arg1 instead of simply passing it as a value. That is, you have an extra set of parens around (arg1 (cdr (cdr arg2)))
. In full, what you want is
(define apply-to-pairs
(lambda (arg1 arg2)
(if (or (equal? (length arg2) 0) (equal? (length arg2) 1))
'()
(cons (arg1 (car arg2) (car (cdr arg2)))
(apply-to-pairs arg1 (cdr (cdr arg2)))))))
Consider formatting your code across more lines so that it's easier to spot these bugs.
Upvotes: 3