Re: stretch function.

Subject: Re: stretch function.
From: Dave Love <>
Date: 25 Sep 1997 17:54:34 +0100
>>>>> "Paul" == Paul Prescod <papresco@xxxxxxxxxxxxxxxx> writes:

 Paul> My solution has two problems. First, it uses map and
 Paul> string->list. 

Using the right tools shouldn't be considered a problem!

 Paul> The second is that it adds a space to the end of the string.

Avoiding explicit recursion is good, but surely we should meet the
specification.  Here's one version that springs to mind:

(define (stretch s)
   (lambda (cs)
     (cdr				; => (char #\space char ...)
      (apply append			; => (#\space char #\space char ...)
		 (map			; => '((#\space char) ...)
		  (lambda (c)
		    (list #\space c))	; char -> (#\space char)
		  cs))))		; (char ...)

;; Convenience: transform string `s' by applying function `f' to a
;; list of the characters
(define (string-via-list f s)
    (list->string (f (string->list s))))

[For string transformations it's a good move to define and use
higher-order functions analagous to the standard's node-list ones
(using intermediate character lists).]

In this case it might be more appropriate to work on the node-list
directly.  Perhaps:

(define (map-node-list->sosofo f nl)
  (if (node-list-empty? nl)
	(f (node-list-first nl))
	(map-node-list->sosofo f (node-list-rest nl)))))

(define (stretch nl)
    (process-node-list (node-list-first nl))
     (lambda (snl)
         (literal " ")
         (process-node-list snl)))
     (node-list-rest nl))))

(element acronym
  (stretch (children (current-node))))

; H T H.

 DSSSList info and archive:

Current Thread