Reputation: 5131
Chapter 4, HtDP.
Note: I've seen this in other questions as well.
Is it for a clarity reason or an algorithmic reason, that I am unaware of, that the base case returns empty instead of the list itself which is empty.
Example:
; List-of-numbers -> List-of-numbers
; compute the weekly wages for all given weekly hours
(define (wage* alon)
(cond
[(empty? alon) empty] ;<---- see here
[else (cons (wage (first alon)) (wage* (rest alon)))]))
; Number -> Number
; compute the wage for h hours of work
(define (wage h)
(* 12 h))
I would think this is just as correct.
; List-of-numbers -> List-of-numbers
; compute the weekly wages for all given weekly hours
(define (wage* alon)
(cond
[(empty? alon) alon] ;<---- see here
[else (cons (wage (first alon)) (wage* (rest alon)))]))
; Number -> Number
; compute the wage for h hours of work
(define (wage h)
(* 12 h))
Upvotes: 2
Views: 89
Reputation: 236004
Both forms are correct and are exactly equivalent, it's just a matter of style. Although it can be argued that this is a bit more clear, because it's more explicit what's being returned:
(if (empty? lst)
empty
...)
In the end, it's a matter of personal taste vs. coding conventions. If you were a member of a team and everybody was using the first form, then you should use it. On the other hand if you're a lone programmer, then use the form that better suits your tastes.
Upvotes: 6