I think you are looking for something like this:

#lang racket

(require test-engine/racket-tests)

(define (remove-empty-lists lst)
  (cond
    [(null? lst) '()] ; Check for end of list
    [(null? (first lst)) (remove-empty-lists (rest lst))] ; Skip null
sublist
    [(not (list? (first lst))) ; Leave non-lists alone
     (cons (first lst) (remove-empty-lists (rest lst)))]
    [else ; Process the sublists recursively
     (cons (remove-empty-lists (first lst))
           (remove-empty-lists (rest lst)))]))

(check-expect (remove-empty-lists '()) '())
(check-expect (remove-empty-lists '(a)) '(a))
(check-expect (remove-empty-lists '(a b c)) '(a b c))
(check-expect (remove-empty-lists '(())) '())
(check-expect (remove-empty-lists '(() a)) '(a))
(check-expect (remove-empty-lists '(a ())) '(a))
(check-expect (remove-empty-lists '(a (b () c) d)) '(a (b c) d))
(check-expect (remove-empty-lists '((a) (b c) () (e () f (g ())) h (() i
j)))
              '((a) (b c) (e f (g)) h (i j)))
(test)

Justin

On Tue, Nov 15, 2016 at 10:52 PM, <meino.cra...@gmx.de> wrote:

> Hi,
>
> I have a list of sublists. Some of the sublists are empty.
> The list should be processed recursevly and the resulting
> list should no longer contain empty sublists.
>
> The code I have so far looks like:
>
> (define (step-through-list lst)
>     (if (empty? lst)
>     lst
>     (cons (process-sublist (car lst)) (step-through-list (cdr lst)))
>
> ....but "car" would feed empty sublists to process-sublist as any other
> sublist. How can I "silently" skip empty sublists and even get out of
> the recursion without harm (if the last sublist is empty) without
> switching to iteration?
> Is there something like "car-not-empty" ? ;)
> Or do I oversee the obvious here...? ;)))
>
> Thank you vary much for any help in advance!
> Cheers
> Meino
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to