Proposed change to libraries

Richard A. O'Keefe ok@REDACTED
Tue Feb 8 01:42:30 CET 2005


I wrote:

	 > Scheme insists on the list argument of map being a proper list, and it
	 > seems to cause no trouble there.
	
Kostis replied:

	I am not sure I get this, because map naturally protects itself from
	inproper lists in its second argument.

It is not true that "map naturally protects itself.  In Scheme,

    (define (map F L)
      (if (pair? L)
          (cons (F (car L)) (map F (cdr L)))
          '()))

is the most obvious way to implement map.  You have to go out of your way
to explicitly check for an empty list.  And that's my point:  in a case
where it would not just be _as_ easy to be "generous" but it would actually
be _easier_ to be "generous", Scheme demands a proper list.

	Can it be that you are mixing
	map with some other function like append?
	
No.  append does *not* require its second argument to be a proper list in
Scheme implementations.  There is an interesting but obvious reason:

    - in cases where the only burden is one extra check "now that this
      argument isn't a pair, is it an empty list?", the check is done.

    - in cases where the burden would be traversing a list that would
      otherwise _not_ have been traversed, the check is not done.

That is, checks are made that don't change the expression inside the big Oh.
Not a bad rule for a library implementor.




More information about the erlang-questions mailing list