Home SICP Lecture 03A
Post
Cancel

SICP Lecture 03A

Write Good Code

The idea of extracting functions needs practice. Here is another example. It multiply each elements in the list by n.

1
2
3
4
5
6
7
(define (scale-list n l)
  (if (null? l)
      nil
      (cons (* (car l) n)
            (scale-list n (cdr l)))
      )
 )

We should write such function using map.

1
2
3
(define (scale-list n l)
  (map (lambda(x) (* n x)) l)
)

When you write your code in this way, you stop to thinking about the control procedure of the program.

This post is licensed under CC BY 4.0 by the author.