I am trying to implement a method that has the ability to square a list that can consists of sub lists (a tree) using map.
This means that (square-tree-map (list 1 3 (list 3 4)))
should return (1 9 (9 16))
.
I came up with this code:
(define (square-tree-map tree)
(define (sq x) (* x x))
(map (lambda (t)
(if (pair? t)
(cons (square-tree-map (car t))
(square-tree-map (cdr t)))
sq t))
tree))
This gives the error:
if: bad syntax; has 4 parts after keyword in: (if (pair? t) (cons (square-tree-mapped (car t)) (square-tree-mapped (cdr t))) sq t)
I only see two possibilities after the if operator, not 4. Why do I get this error?