Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!samsung!crackers!m2c!umvlsi!dime!cs.umass.edu!pop From: pop@cs.umass.edu Newsgroups: comp.lang.lisp Subject: Mapping is elegant. Message-ID: <26894@dime.cs.umass.edu> Date: 19 Feb 91 19:18:39 GMT References: Msg 3072 of comp.lang.lisp Sender: news@dime.cs.umass.edu Reply-To: pop@cs.umass.edu () Organization: University of Massachusetts, Amherst Lines: 73 > Subject: lost soul needs help with trivial code. The neatest solutions to these problems usually employ some kind of mapping function. These also have the nice property that they lend themselves well to automatic theorem proving. E.g. see the work of Stemple and Sheard, following Boyer and Moore. The solutions below are for Common Lisp, POP-11 and ML. I am not enough of a LISP hacker to know whether Currying can be done in that language. Of course, there is no need actually to name the diff-pair function in any of the languages - use some lambda-form. The main difference between POP-11 and ML in this respect is that POP-11 requires the explicit (%...%) notation for Currying (and it is on the last argument). Note the ML warning - a nice feature of that language. It tells you that the patterns for the function arguments are not exhaustive, so that it would be an error to apply it to e.g. an empty list, or one with one member only. =========================================================================== Program in Common Lisp =========================================================================== (defun diff-pair (pr) (- (cadr pr) (car pr))) (defun make-length-list(coord) (mapcar #'diff-pair coord)) (make-length-list '((100 200)(300 400)(455 526))) --------------------------------------------------------------------------- OUTPUT ---------------------------------------------------------------------------- DIFF-PAIR MAKE-LENGTH-LIST (100 100 71) =========================================================================== Program in POP-11 ========================================================================== define diff_pair(p); p.tl.hd - p.hd enddefine; vars make_length_list = maplist(%diff_pair%); ;;; Make function by partial ;;; application or Currying. make_length_list([[100 200] [300 400] [455 526]]) => ---------------------------------------------------------------------------- OUTPUT ---------------------------------------------------------------------------- ** [100 100 71] =========================================================================== Program in ML =========================================================================== fun maplist1 F [] = [] | ;;; Define maplist1 for maplist1 F (x::L) = F x :: maplist1 F L; ;;; subsequent Currying. fun diff_pair (i::j::[]) = j-i:int; ;;; Define pair-differencing val make_length_list = maplist1 diff_pair; ;;; Curry it up make_length_list [[100,200],[300,400],[455,526]]; ---------------------------------------------------------------------------- OUTPUT ---------------------------------------------------------------------------- val maplist1 = fn : ('a -> 'b) -> 'a list -> 'b list ;;; ML WARNING - Clauses of function declaration are non-exhaustive ;;; CONTEXT : fun diff_pair val diff_pair = fn : int list -> int val make_length_list = fn : int list list -> int list [100, 100, 71] : int list ===========================================================================