learn
learn

Reputation: 13

List of lists in racket

I need to create this:

Define a min&max-lists function that consumes a list of lists (where the type of the elements in the inner list may be any type).

The function returns a list of lists – such that for each inner list (in the original list) the following is done –

  1. If the list contains at least one number, then the list is replaced with a list of size two, containing the minimum and maximum in the list.
  2. Otherwise, the list is replaced with a null.

For example

written in a form of a test that you can use:

(test (min&max-lists '((any "Benny" 10 OP 8) (any "Benny" OP (2 3)))) 

=> '((8 10) ())) (test (min&max-lists '((2 5 1 5 L) (4 5 6 7 3 2 1) ())) >> '((1 5) (1 7) ()))

For now, I have created a function that do it for one list.

How I do it for the list of lists??

for example:

(listhelp '(2 5 1 5 L)) 
-> : (Listof Number)>>'(1 5)

Upvotes: 0

Views: 4019

Answers (1)

Sylwester
Sylwester

Reputation: 48745

Given that you have min&max with the strange name listhelp you can use map, use for/list, or roll your own recursion:

(define (min&max-lists lol)
  (map min&max lol))

(define (min&max-lists lol)
  (for/list ([e (in-list lol)])
    (min&max e)))

(define (min&max-lists lol)
  (if (null? lol)
      '()
      (cons (min&max (car lol))
            (min&max-lists (cdr lol)))))

Upvotes: 0

Related Questions