Search code examples
mit-scheme

In MIT Scheme, What is the difference between (null? lst) and (null? (cdr lst))?


I am currently going through SICP and I am having a hard time understanding the difference between the two expressions below. Assume we have a list, called lst, What is the difference between:

(null? lst) and (null? (cdr lst))

I know that the first expression checks if the list is empty. But doesn't the second expression check for the same condition as well? In other words checks if the rest of the list is empty.


Solution

  • No, they are no the same thing, (null? lst) just checks whether lst is empty or not. Whereas(null? (cdr lst)) checks whether lst has only one element, since (cdr lst) returns lst with everything but the first element. See examples below.

    > (null? '())
    #t
    
    > (null? '(1))
    #f
    
    > (null? (cdr '(1)))
    #t
    
    > (null? (cdr '(1 2)))
    #f