Reputation: 4489
I have a function that takes a list of arguments with variable length. And depend on its length, i have to invoke another function, but with different length of arguments. You see the function below:
(define (set-contents . args)
(define columns-length (length args))
(cond
((= columns-length 1)
(send output-list set (empty-list rows-length) (get-nth-item columns-as-list 0)))
((= columns-length 2)
(send output-list set (empty-list rows-length) (get-nth-item columns-as-list 0) (get-nth-item columns-as-list 1)))
((= columns-length 3)
(send output-list set (empty-list rows-length) (get-nth-item columns-as-list 0) (get-nth-item columns-as-list 1) (get-nth-item columns-as-list 2)))
((= columns-length 4)
(send output-list set (empty-list rows-length) (get-nth-item columns-as-list 0) (get-nth-item columns-as-list 1) (get-nth-item columns-as-list 2) (get-nth-item columns-as-list 3)))
This cond clause can be still longer. It is working but absolutely no proper.
Is there a better way to fill up this function?
Upvotes: 2
Views: 320
Reputation: 1413
Use apply?
(define (set-contents . args)
(define columns-length (length args))
(apply send (append (list output-list set '())
(take column-length columns-as-list))))
(define (take num lst)
(cond ((< num 1) '())
((null? lst) (error "Cannot TAKE from the empty list")
(else (cons (car lst) (take (- num 1) (cdr lst))))))
Sure beats being a human compiler
Upvotes: 0
Reputation: 223193
Well, a direct translation of the idea is as follows:
(define (set-contents . args)
(send/apply output-list set (empty-list rows-length)
(for/list ((i (length args)))
(get-nth-item columns-as-list i))))
But, since you're not actually using the arguments other than getting the argument count, that seems pretty fishy. What are you trying to do?
Upvotes: 2