aleix's blog

The Law of Car

10 April 2013 8:18 am (scheme | guile)

In The Little Schemer, The Law of Car is defined as:

The primitive car is defined only for non-empty lists.

In the implementation given for (firsts l), it seems to me that the law is broken:

(define firsts
  (lambda (l)
    (cond
      ((null? l) '())
      (else (cons (car (car l))
                  (firsts (cdr l)))))))

As (firsts '()) is '(). So, this would actually fail (in guile):

$ (firsts '((a b) () (e f)))
In procedure firsts:
In procedure car: Wrong type argument in position 1 (expecting pair): ()

I think a correct implementation would be (note that there's no need to write the second cond as the first cond could take multiple clauses):

(define (firsts l)
  (cond
   ((null? l) '())
   (else (cond
          ((null? (car l)) (firsts (cdr l)))
          (else
           (cons (car (car l))
                 (firsts (cdr l))))))))

in which we take care of the non-empty list before getting the first typical element (car (car l)). This would result in:

$ (firsts '((a b) () (e f)))
(a e)

9 responses

  1. assignment writing service says:

    Thank you for your post. I think this post definitely will help students ,especially for programing students.I hope students will easily catch your ideas so that it would become the step by step blog.

  2. Sociology Online Assignment Help says:

    Your blog is filled with unique good articles! I was impressed how well you express your thoughts and sharing your experiences.I never regretted any moment that came to your blog! :)

  3. MyAssignmentHelp review says:

    Cool blog! Thanks for sharing this material, never knew about Law of Cars. I only started to learn to code, so that was very useful for me.

  4. WaltonWilliam says:

    This is by and by related with the undertaking of sizes and courses of action to programming-tongue sorts. Another solidly related point is deforming. Calling conventions, sort portrayals, Buy custom Essays and name desolating are all piece of what is known as an application twofold interface.

  5. Stats Assignment help says:

    Things are very open and intensely clear explanation of issues. was truly information. Your website is very beneficial.

  6. 项目写作帮助 says:

    事情是非常开放和强烈清楚的解释问题。是真正的信息。你的网站是非常有益的。

  7. Write My Essay For Me says:

    Saved as a favorite, I love your web site!

  8. Write My College Paper For Me says:

    Aw, this was a really nice post. Spending some time and actual effort to generate a great article but what can I say… I put things off a whole lot and don't seem to get anything done.

  9. Star Wars Han Solo Jacket Buy Movie Jackets says:

    Car wellbeing is the examination and routine with regards to plan, development, hardware and direction to limit the event and outcomes of car accidents. Street activity wellbeing all the more comprehensively incorporates roadway outline.
    One of the main formal scholarly examinations into enhancing vehicle security was by Cornell Aeronautical Laboratory of Buffalo, New York. The primary finish of their broad report is the essential significance of safety belts and cushioned dashboards.

Leave a Reply