Search code examples
lispcommon-lispclisp

Applying sqrt function to multiple lists in clisp


Newbie trying to learn Lisp. I want to apply sqrt (or any function) to several lists in Clisp. For eg. using mapcar we can apply to one list such as (mapcar #'sqrt ( 10 20 30)).

But what about cases where the lists are ((10 20) (30 40) (50)). Thanks in advance for the help.


Solution

  • Try

    ? (mapcar (lambda (e) (mapcar #'sqrt e)) '((10 20) (30 40) (50)))
    ((3.1622777 4.472136) (5.477226 6.3245554) (7.071068))
    

    For arbitrary depths, you could use a recursive function:

    (defun rmap (fun lst)
      (mapcar
       (lambda (x)
         (if (listp x)
           (rmap fun x)
           (funcall fun x)))
       lst))