From: ยทยทยท@cs.umass.edu
Subject: Mapping is beautiful
Date: 
Message-ID: <26901@dime.cs.umass.edu>
[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

===========================================================================