[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Issue: FUNCTION-COMPOSITION (Version 4)



    Date: 12 Dec 88 11:04 PST
    From: cl-cleanup@sail.stanford.edu


    !
    Forum:          Cleanup
    Issue:          FUNCTION-COMPOSITION
    References:     None
    Category:       ADDITION
    Edit history:   21-Jun-88, Version 1 by Pitman
		    05-Oct-88, Version 2 by Pitman
		     7-Dec-88, Version 3 by Masinter
		    12-Dec-88, Version 4 by Masinter (additional comments)
    Related-Issues: TEST-NOT-IF-NOT

    Problem Description:

      A number of useful functions on functions are conspicuously
      absent from Common Lisp's basic set. Among them are functions
      which return constant T, constant NIL, and functions which
      combine functions in common, interesting ways.

    Proposal (FUNCTION-COMPOSITION:NEW-FUNCTIONS):

      Add the following functions:

       COMPOSE &REST functions				[Function]

	Returns a function whose value is the same as the composition
	of the given functions. eg, (FUNCALL (COMPOSE #'F #'G #'H) A B C)
	is the same as (F (G (H A B C))). Also, for example, #'CAADR may
	be described as (COMPOSE #'CAR #'CAR #'CDR).

      (DEFUN COMPOSE (&REST FUNCTIONS)
	(COND ((NOT FUNCTIONS)
	       (ERROR "COMPOSE requires at least one function."))
	      ((NOT (REST FUNCTIONS))
	       (FIRST FUNCTIONS))
	      (T
	       (LET ((REVERSED-FUNCTIONS (REVERSE FUNCTIONS)))
		 (LET ((LAST-FUNCTION   (FIRST REVERSED-FUNCTIONS))
		       (OTHER-FUNCTIONS (REST  REVERSED-FUNCTIONS)))
		   #'(LAMBDA (&REST ARGUMENTS)
		       (DO ((O OTHER-FUNCTIONS (CDR O))
			    (VAL (APPLY LAST-FUNCTION ARGUMENTS)
				 (FUNCALL (CAR O) VAL)))
			   ((NULL O) VAL))))))))

This example implementation is not consistent with the description.  The
description doesn't say that at least one function is required.  I think
that (COMPOSE) should return #'IDENTITY, which is the identity of the
COMPOSE function.

Stylistic note: Both instances of "NOT" in the above function definition
should be "NULL", since they are testing for a list being empty, not a
boolean being false.

                                                barmar