Need some help..

Discussion of Common Lisp

Re: Need some help..

Postby Johny22 » Fri Oct 30, 2009 9:21 am

I do not know the concept of nested list, can you please explain a little ?
Johny22
 
Posts: 11
Joined: Thu Oct 29, 2009 5:36 am

Re: Need some help..

Postby Ramarren » Fri Oct 30, 2009 9:32 am

Johny22 wrote:I do not know the concept of nested list, can you please explain a little ?


I hope this is mostly a language barrier, because this is rather fundamental to programming.

A list is an ordered set of objects. Lists are nested when one of the object contained by a list is a list itself. Lisp uses a syntax for lists where they are delimited by parentheses and contained objects are separated by whitespace.

So in the
Code: Select all
(+ (* a b)(/ c a ))

example there is a list of three objects, two of which are lists themselves, each of which contain three objects. In the prefix syntax for expressions, the first element of a list, sometimes called `head`, is an operator, and the remaining elements are its arguments. Hence, there are are no operators 'in the middle' of the expression, because they all are in front of a list, even if their particular list is inside, or nested, in another list.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Example recursive function

Postby nuntius » Fri Oct 30, 2009 9:40 am

This example shows one way of traversing nested lists, dispatching on what you find, and collecting results.

Code: Select all
(defun recur-sum (list)
  "sum the numbers in a tree of lists, return (values sum count)"
  (let ((sum 0)
        (count 0))
    (dolist (x list)
      (cond
        ((symbolp x)
         (error "Can't add symbol ~A" x))
        ((null x)) ;; optimization: ignore empty lists
        ((listp x)
         (multiple-value-bind (subsum subcount)
             (recur-sum x)
           (incf sum subsum)
           (incf count subcount)))
        ((numberp x)
         (incf sum x)
         (incf count 1))
        (t
         (error "Can't add ~A ~A" (type-of x) x))))
    (values sum count)))

;; test case
(trace recur-sum)
(recur-sum '(1 2 (3 4) 5))
(untrace recur-sum)


If recur-sum took an "&optional (count 0)" parameter and recurred using "(recur-sum x count)", then the recursive calls would know how many items were before them.
User avatar
nuntius
 
Posts: 498
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: Need some help..

Postby Johny22 » Sat Oct 31, 2009 9:20 am

For the second problem with nth var, how can i use delete-if ?

i tried writing some code but it didn't work :(, can someone help me with this code and correct it if necesary ?
Code: Select all
(defun nth_var (el l)
   (if (null l) '()
      (and (delete-if-not #' (lambda (x) alpha-char-p (x)) (l))(delete-if-not #' (lambda (x) alphanumericp (x)) (l))
            (nth el (l))
      )
   )
)


I tried to modify the list so the math operators will be removed and than to get the nth element from the list.

I also need it to be a macro, how can i do that ?

PS: I tried that flattening method but nothing, so i tried this method but this one also doesn't work.
Johny22
 
Posts: 11
Joined: Thu Oct 29, 2009 5:36 am

Re: Need some help..

Postby Johny22 » Sat Oct 31, 2009 1:36 pm

for the problem where i need to get the nth variable from an expresion, I found how to flatten the list :), here is the code i used (i used the loop function :)):

Code: Select all
(defun flatten (list)
   (loop for i in list if (listp i) append (flatten i) else collect i)
   
)


Now i want to use the alpha-char-p and alphanumericp functions with the remove-if function so i can remove all math operators from my flattened list and than to use the nth function so i can get the nth element from my final list. Can someone pls help me with this ? :D

PS: It needs to be a macro.
Johny22
 
Posts: 11
Joined: Thu Oct 29, 2009 5:36 am

Re: Need some help..

Postby Johny22 » Sun Nov 01, 2009 8:01 am

I've wrote these 3 functions :

Code: Select all
(defun flatten (list)
"Flatten the list"
   (if (null list) '()
      (loop for i in list if (listp i) append (flatten i) else collect i)
   )
)
(defun rem_mo (list)
"Remove the math operators"
   (if (null list) '()
      (delete-if-not #'alphanumericp list)
   )
)
(defun nth_var (el list)
"Get the nth variable from the list"
   (if (null list) '()
      (nth el list)
   )
)


Can someone show me how to make them work together ? I want to flatten the list gotten as an argument, tham to remove the math operators and finaly get the nth element that also was given as argument. All this needs to be a macro. Anyone knows how to do this ?
Johny22
 
Posts: 11
Joined: Thu Oct 29, 2009 5:36 am

Re: Need some help..

Postby nuntius » Sun Nov 01, 2009 3:08 pm

Hint: you want the nth_var of the rem_mo of the flattened list.

Why do you need this to be a macro? Other than defmacro instead of defun, macros use the same syntax as functions; they just get their arguments unevaluated and can't easily be used at runtime. So (fun '(1 2 3)) becomes (mac (1 2 3)).
User avatar
nuntius
 
Posts: 498
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: Need some help..

Postby Johny22 » Tue Nov 10, 2009 2:24 am

I made them work :d, thanks to all that tried to help me :D
Johny22
 
Posts: 11
Joined: Thu Oct 29, 2009 5:36 am

Previous

Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron