[plt-scheme] List comprehension style programming
This one-liner is what an HtDP student with full knowledge of ISL
would write:
;; myfunc :: list of list of numbers -> number
;; returns the number of numbers in the list which has the most non-
zero numbers
(check-expect (myfunc '((0 0 1 0) (1 2 0 1) (0 1 1 0))) 3)
(check-expect (myfunc '((0 0 1 0) (1 2 0 1) (0 1 1 0 8 2))) 4)
(define (myfunc ll)
(apply max (map (lambda (l) (length (filter (compose not zero?)
l))) ll)))
-- Matthias
On May 7, 2009, at 6:10 PM, Sam TH wrote:
> On Thu, May 7, 2009 at 6:00 PM, wooks <wookiz at hotmail.com> wrote:
>> General question. How deep should you layer invocations of HOF's.
>
> I would write this with the `for' set of list comprehensions/loops.
> Here's your example, with no extra traversals:
>
> (define (f l)
> (for/fold ([k 0])
> ([e l])
> (max k
> (for/fold ([k 0]) ([i e] #:when (not (zero? i))) (add1 k)))))
>
> (f '((0 0 1 0) (1 2 0 1) (0 1 1 0)))
> (f '((0 0 1 0) (1 2 0 1) (0 1 1 0 8 2)))
>
> --
> sam th
> samth at ccs.neu.edu
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme