[plt-scheme] help with higher order functions

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Mar 18 10:49:21 EDT 2009

Following part IV of HtDP, plus a guess at multi-argument functions  
in a () - language:

#lang scheme

(require test-engine/scheme-tests)

;; add-lists: (listof number) (listof number) -> (listof number)
;; consumes: two lists of numbers
;; produces: the item-wise sum (ala vector addition)
(define (add-lists.v1 l1 l2)
   (cond
     [(empty? l1) empty]
     [(cons? l1) (cons (+ (first l1) (first l2)) (add-lists (rest l1)  
(rest l2)))]))

(define (add-lists l1 l2) (foldr (lambda (x y r) (cons (+ x y) r))  
'() l1 l2))

(check-expect (add-lists '(1 2 3) '(4 5 6)) '(5 7 9))

(test)




On Mar 18, 2009, at 10:42 AM, Todd O'Bryan wrote:

> What is the proper Scheme idiom to write this function?
>
> ;; add-lists: (listof number) (listof number) -> (listof number)
> ;; consumes: two lists of numbers
> ;; produces: the item-wise sum (ala vector addition)
> (define (add-lists l1 l2)
>   (cond
>     [(empty? l1) empty]
>     [(cons? l1) (cons (+ (first l1) (first l2))
>                              (add-list (rest l1) (rest l2)))]))
>
> I guess what I want is something like a zip function. There must be a
> standard way to process lists in parallel, but I don't know what it
> is.
>
> Todd
>
> P.S. Still working on the nifty assignment. I got distracted by
> figuring out how to do bitmaps.
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.