Question

I want to define a function which takes a number of parameters which can be changed.

I thought that my function should take one parameter as a list and then parse that but it didn't work or I wrote it wrong.

Calling of the function will vary as belov.I thought to define the function as

(define function (lambda(list)(... 

then work on list but it didn't work out.Thanks

(function param1 '(2 3) '(3 1))

(function param1 '(2 3) '(3 2) '(1 5))
Was it helpful?

Solution

There is a syntax for this, where all parameters will be automatically put into a list:

(define function
  (lambda lst ; no parentheses!
    lst))

or the equivalent

define (function . lst) ; mind the dot!
    lst)

then

(function '(2 3) '(3 1))
=> '((2 3) (3 1))
(function '(2 3) '(3 2) '(1 5))
=> '((2 3) (3 2) (1 5))

Inside your function (in my example I simply return the list) you can then process the list with higher-order functions (map, filter, ...) or dissect them with first, second, or similar.

You can also have a number of fixed parameters first, and then the variable part, example:

(define (function p1 p2 p3 . lst) ; p1-3 are fixed
    (values p1 p2 p3 lst))

(function 'this 'is 'fixed '(2 3) '(3 1))
=> 
'this
'is
'fixed
'((2 3) (3 1))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top