example not very clear on 'apply'...

Discussion of Common Lisp

example not very clear on 'apply'...

Postby megera » Sun Oct 14, 2012 6:06 am

HI
I'm reading an example of code that use apply built-in..but a step isn't clear to me:
ps. this function take a compress list as ((3 1) 0 1 (4 0) 1)) and return (1 1 1 0 1 0 0 0 0 1)

Code: Select all
(defun uncompress (lst)
      (if (null lst)
           nil
          (let ((elt (car lst))
           (rest (uncompress (cdr lst))))
        (if (consp elt)
            (append (apply #' list_of elt)
              rest)
            (cons elt rest)))))

(defun list_of (n elt)
      (if (zerop n)
           nil
          (cons elt (list_of (- n 1) elt))))



well," list_of" func take two arg of course, but why when "uncompress" func give it only one arg through apply ( for ex. when elt value is one integer and not a nested list of pair) this function doesn't raise error???
thanks again in advance!!
ps. sorry for wrong inidentation in the first defun (the second if belongs to let declaration), in preview works, but not when printed....(???)
Last edited by megera on Sun Oct 14, 2012 6:36 am, edited 1 time in total.
megera
 
Posts: 30
Joined: Wed Oct 10, 2012 1:34 pm

Re: example not very clear on 'apply'...

Postby Goheeca » Sun Oct 14, 2012 6:33 am

The elt argument is never an integer because of testing on consp.
and this raises an error for me:
Code: Select all
(defun test (a b) (+ a b))
(apply #'test 1)
(apply #'test 1 nil)
(apply #'test '(1))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 165
Joined: Thu May 10, 2012 12:54 pm

Re: example not very clear on 'apply'...

Postby megera » Sun Oct 14, 2012 6:48 am

ohhhh!!thanks!! my distraction! sorry ;)
(car lst) in this example can return or integer or a nested list!!, then only when return a list it'll valued true by consp...
I wrongly considered also the case like '(1) but car never return that in this example...
thanks for your help!
megera
 
Posts: 30
Joined: Wed Oct 10, 2012 1:34 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 4 guests