0

I'm wondering, is there any implementation of map-filter in CPS version in Scheme?

example:

(map-filter square even? '(1 2 3 4)) -> '(4 16)  
Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • This question is very unclear, and it's also not clear how your example is related to CPS. Could you clarify what you mean? – molbdnilo Aug 11 '20 at 11:26

2 Answers2

1

You can define map-filter& using filter& and map&:

(define (map-filter& proc& pred?& lst k)
  (filter& pred?&
           lst
           (lambda (new-lst)
             (map& proc& new-lst k))))
Sylwester
  • 47,942
  • 4
  • 47
  • 79
1

[This answer give two versions of the function the person described: not CPS versions of it. I think the question is ambiguous but this is probably not the right answer: Sylwester's is.]

If what you want is what you've described (which I'm fairly sure is not CPS), then something like this works:

(define (map-filter mapper filterer lst)
  (map mapper (filter filterer lst)))

In Racket then this might be better as this:

(define (map-filter mapper filterer lst)
  (for/list ([e (in-list lst)]
             #:when (filterer e))
    (mapper e)))

which perhaps has a better chance of avoiding consing an intermediate list but smells nastier to me.

  • 2
    None of these are [CPS](http://matt.might.net/articles/cps-conversion/) – Sylwester Aug 12 '20 at 11:37
  • @Sylwester: I agree (or not in any CPS I'm familiar with). I had assumed that the person was after a function with the signature they gave, but I think I'm wrong now: they knew how to write that function but wanted a CPS conversion of it. –  Aug 12 '20 at 13:16