XLISP > XLISP Plus  -  Previous | Next

mapcan


Type:   -   defined macro (closure)
Source:   -   init.lsp

Syntax

(mapcan function list1 [list2 ... ])
function - a function definition like a lambda form or a function name
listN - a list or list expression
returns - a list from the results of the function applications

Description

The 'mapcan' macro applies the 'function' to the succesive cars of each of the lists 'listN'. Each of the lists supplies one of the arguments to 'function'. 'mapcan' is similar to mapcar, except that the 'mapcan' macro returns a list that is constructed via the destructive nconc function from the results of the 'function' applications. If the lists are of different lengths, the shortest list will determine the number of applications of 'function'.

(defmacro mapcan (&rest args)
  `(apply #'nconc (mapcar ,@args)))

Note: The 'mapcan' macro is intended to simplify porting code from Common Lisp to XLISP. This macro is not included in the standard Nyquist distribution. If you want to use it, copy the code above to your 'init.lsp' file.

Examples

(mapcar 'list '(1 2 3) '(a b c) )        ; returns ((1 A) (2 B) (3 C))
(mapcan 'list '(1 2 3) '(a b c) )        ; returns (1 A 2 B 3 C)
(mapcan 'list '(a b c) '(1 2 3 4 5 6))   ; returns (A 1 B 2 C 3)

Note: Remember that 'mapcan' uses nconc and so it deals with its list arguments destructively. It is often used when you want to remove NIL entries from the resulting list, because nconc will take out the NILs.

Note: The use of the 'function' will work properly when it is a quoted symbol [the name of the function], an unquoted symbol whose value is a function or a closure object like a lambda form.

See also the XLISP Plus mapcon function in the XLISP reference manual.


XLISP > XLISP Plus  -  Previous | Next