Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!think.com!samsung!crackers!m2c!umvlsi!dime!cs.umass.edu!pop From: pop@cs.umass.edu Newsgroups: comp.lang.lisp Subject: Mapping is beautiful Message-ID: <26901@dime.cs.umass.edu> Date: 19 Feb 91 20:21:20 GMT Sender: news@dime.cs.umass.edu Reply-To: pop@cs.umass.edu () Organization: University of Massachusetts, Amherst Lines: 65 [This posting seems to have got lost, so I hope I am not repeating it] > 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. All explicit definitions of diff_pair can of course be replaced by the lambda- forms of the various languages. =========================================================================== 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 ===========================================================================