Q:

Simple Scheme, squaring all elements in a list

Okay, I'm new to Scheme and I thought I understood it, but got confused on this problem.  I want to square all the elements of a list.  So, (mapsq '(1 2 3)) returns (list 1 4 9).
my code:
(define mapsq
  (lambda (ls)
    (cond ((null? ls) 0)
          (else (cons (car ls) (car ls))
                (mapsq (cdr ls)))))))

A:

In a practical (non-academic) context, this problem can be easily solved by using the map procedure:
(define mapsq
  (lambda (ls)
    (map (lambda (x) (* x x))
         ls)))

Of course, if this is homework and you need to implement the solution from scratch, I shouldn't spoon-feed the answer. Better find out the solution by yourself, filling-in the blanks:
(define mapsq
  (lambda (ls)
    (cond ((null? ls)               ; If the list is empty
           <???>)                   ; ... then return the empty list.
          (else                     ; Otherwise
           (cons (* <???> <???>)    ; ... square the first element in the list
                 (mapsq <???>)))))) ; ... and advance the recursion.

There are two problems in your solution: first, the base case should not return 0 - if we're building a list as an answer, then you must return the empty list. Second, in the recursive step you aren't actually squaring the current element in the list - to do that just multiply it by itself with the * operator.

