[plt-scheme] HTDP 21.1.2 - trying to put it together

From: wooks . (wookiz at hotmail.com)
Date: Mon Jul 17 11:07:01 EDT 2006

>From: Chris Warrington <chrisw at rice.edu>
>Reply-To: Chris Warrington on the PLT Scheme List 
><plt-scheme at list.cs.brown.edu>
>To: wooks on the PLT Scheme List <plt-scheme at list.cs.brown.edu>
>Subject: Re: [plt-scheme] HTDP 21.1.2 - trying to put it together
>Date: Sun, 16 Jul 2006 16:35:21 -0400
>
>
>wooks . @ 2006-7-16 2:06:12 PM
>"[plt-scheme] HTDP 21.1.2 - trying to put it together" 
><mid:BAY103-F11E6EFF0190AF0B9751C84C06D0 at phx.gbl>
>
> >>> I've "lost" the (X -> Y) ...the only thing I can think of now is
> >>> making somefunc local to the definition of the map function to
> >>> bring the (X -> Y) from map within the scope of somefunc.
> >> What happens if you do that?
> > It works.
>
> > (define (myMap func a-list)
> >   (local ((define (someFunc x aloy)
> >      (cons (func x) aloy)))
> >     (fold someFunc empty a-list)))
>
>Now, can you extend what you learned when writing map to write
>something like filter?
>
>filter : (X -> boolean) [X] -> [X]
>

more  prolog notation

;; fold   : (X Y -> Y) Y [X]  ->  Y
;; filter : (X -> boolean) [X] -> [X]

;; fold   : (X [X] -> [X]) [X] [X] -> [X]
(define (myFilter func? a-list)
  (local ((define (filterFunc x alox)
            (cond
              [(func? x) (cons x alox)]
              [else alox])))
  (fold filterFunc empty a-list)))

(filter number? '(8 2 3 o d h oc 82 g b 92 ))




Posted on the users mailing list.