Need some help..
Re: Need some help..
I do not know the concept of nested list, can you please explain a little ?
Re: Need some help..
I hope this is mostly a language barrier, because this is rather fundamental to programming.Johny22 wrote:I do not know the concept of nested list, can you please explain a little ?
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 recursive function
This example shows one way of traversing nested lists, dispatching on what you find, and collecting results.
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.
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)
Re: Need some help..
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 ?
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.
i tried writing some code but it didn't work

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 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.
Re: Need some help..
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
):
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 ? 
PS: It needs to be a macro.


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

PS: It needs to be a macro.
Re: Need some help..
I've wrote these 3 functions :
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 ?
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)
)
)
Re: Need some help..
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)).
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)).
Re: Need some help..
I made them work :d, thanks to all that tried to help me 
