Hey, I’m working on a homework question about applying a function to every element in a list, going as deep as needed.
I am getting a error when calling (fun (car l)), that
mcar: expects argument of type <mutable-pair>; given 5
and yet when I just call (fun l) i get the error
+: expects type as 1st argument, given: (5); other arguments were: 1
(define (map-gen fun l)
(if (null? fun) l
(if (null? l) '()
(if (list? (car l))
(append (map-gen fun (cdr l)) (map-gen fun (car l)))
(append (map-gen fun (cdr l)) (fun (car l)))))))
Any and all help is appreciated!
edit: This is when calling the function like so:
(map-gen (lambda (x) (+ x 1))'(1 (2 (3 4))(((5)))))
You’ve got the arguments for appends backwards, and I think cons would be a better bet. Also, checking to see if fun is null is unnecessary.
Also, as Eli says in his answer, you really should look into cond instead of nested ifs. It’s much more elegant, making your code more readable.