0

This function is supposed to accept as many lists that are input and append them all together, as the name implies.

(AppendAll ( L1   L2 …  Ln))

I'm having trouble figuring out how to do the recursive definition.

If I am correct, the base case would be:

((null? L)    '())

I basically don't know how to accept an uncertain amount of lists as parameters. basically Ln lists.

  • Before answering, please make a little more effort - ok, you got the base case, what about the other case? how would you _append_ each list to the next? Update your answer with the code. – Óscar López Mar 14 '17 at 02:11
  • @ÓscarLópez that's what I am asking, I'm unsure how to create the method to accept an unknown amount of lists – Age of the Milk Mar 14 '17 at 02:37
  • Stack Overflow is not a code writing service, you _have to_ put more effort into your questions (that means: post more code!), otherwise people will think you just want somebody else to do your homework... – Óscar López Mar 14 '17 at 02:41
  • FYI: here's a [post](http://stackoverflow.com/q/12658406/201359) explaining how to deal with an unspecified number of parameters. – Óscar López Mar 14 '17 at 03:36

2 Answers2

0

apply, apply is the answer you are seeking.

Function which take arbitrary number of parameter can't be called using the function name.

(define L '(1 2 3))
(+ L) ;doesn't work

You need to apply + over L.

(apply + L) ;evaluates to 6

Now, For your current problem,

(define (append l1 l2)
  (cond
    ((null? l1) l2)
    (else (cons
           (car l1)
           (append (cdr l1) l2)))))
(define append_all
  (lambda x
    (cond
      ((null? x) '())
      (else
       (append (car x)
               (apply append_all (cdr x)))))))
(append_all '(1) '(2) '(2 3))
Shakil Ahamed
  • 587
  • 3
  • 18
0

How to accept uncertain amount of arguments is by using rest argument. ALmost all languages has this and in Scheme you use a variable instead of list with a variable in it. eg.

(define my-list  
  (lambda arguments
    arguments))
(my-list 1 2 3 4)
; ==> (1 2 3 4) 

Also the shorthand define for functions which is the same:

(define (my-list . arguments)
  arguments)

Imagine that you want a function that takes 2 arguments and in addition an unknown amount of arguments after that:

(define (test a b . rest)
  (list a b rest))

(test 1 2 3 4 5)
; ==> (1 2 (3 4 5))

Also the opposite mirror operation is perhaps interesting, which is apply. Imagine you want (+10 1 2 3) ; ==> 16 and that it should take any number of arguments. It can be done like this:

(define (+10 . args)
  (apply + 10 args))

Apply takes a function, then the second until the next to last arguments (may be none) as the first arguments to that function and then the last argument, which is a zero or more element proper list, as the last arguments to the function. Thus:

(apply + 1 2 3 '(4 5 6)) ; ==
(+ 1 2 3 4 5 6)

A common pearl of a function made with this is zip:

(define (zip . args)
  (apply map list args))

(zip '(1 2 3 4) 
     '(a b c d) 
     '(I II III IV))
; ==> ((1 a I) 
;      (2 b II) 
;      (3 c III) 
;      (4 d IV))

And you have unzip, which does the same as zip except it takes a list of lists as one argument. Passing the result through unzip twice recreates the one agrument:

(define (unzip lsts)
  (apply zip lsts))

(unzip '((1 a) (2 b)))
; ==> ((1 2) (a b))
(unzip (unzip '((1 a) (2 b))))
; ==> ((1 a) (2 b))
Sylwester
  • 47,942
  • 4
  • 47
  • 79