counting non-numeric characters.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
counting non-numeric characters.
Hello everyone
I am very very new to Lisp. We only had 3 cases so far on lisp. This is out second assignment and i am a bit lost. Any help is appreciated. Please keep in my i have to do everthing with recursion. I can't use any variables except for the parameters.
Write a function called "notnums" that will take a list of arbitrary length and nesting complexity and return the number of non-numbers the list contains, anywhere (i.e., at any level of nesting) in the list. Your function should be able to handle the following input:
* (notnums '(a 9 (5 b) c (d (e 7))))
5
* (notnums '(7 a 8 b c 9 10))
3
* (notnums '(1 (2 (3)) y 5 (x) 9))
2
* (notnums '(a 1 (b 2) ())
2
* (notnums '(* b (a (- 2) c 3) (+ 5))
6
So far i have
(defun notnums (A)
(if (endp A)
0
(1+(notnums (rest A))))
)
This is the function i wrote last week to count the number of atoms in a list. I am trying to modify it for this assignment but i am totally lost.
Thanks.
I am very very new to Lisp. We only had 3 cases so far on lisp. This is out second assignment and i am a bit lost. Any help is appreciated. Please keep in my i have to do everthing with recursion. I can't use any variables except for the parameters.
Write a function called "notnums" that will take a list of arbitrary length and nesting complexity and return the number of non-numbers the list contains, anywhere (i.e., at any level of nesting) in the list. Your function should be able to handle the following input:
* (notnums '(a 9 (5 b) c (d (e 7))))
5
* (notnums '(7 a 8 b c 9 10))
3
* (notnums '(1 (2 (3)) y 5 (x) 9))
2
* (notnums '(a 1 (b 2) ())
2
* (notnums '(* b (a (- 2) c 3) (+ 5))
6
So far i have
(defun notnums (A)
(if (endp A)
0
(1+(notnums (rest A))))
)
This is the function i wrote last week to count the number of atoms in a list. I am trying to modify it for this assignment but i am totally lost.
Thanks.
Re: counting non-numeric characters.
You need two more predicates (functions that return true or false), one for numbers and one for lists.
Use these predicates to control when you recurse and what value you return at the leaf nodes...
Use these predicates to control when you recurse and what value you return at the leaf nodes...
Re: counting non-numeric characters.
Thank you very much for the help. I almost got the code working. I still have one problem and i can't seem the solve.
This is my current code.It works great for single lists but when i try to use it nested list it doesn't go after the inside list.
Break 52 [56]> (notnums '( 5 1 (2) 3 s 4 ))
44. Trace: (NOTNUMS '(5 1 (2) 3 S 4))
45. Trace: (NOTNUMS '(1 (2) 3 S 4))
46. Trace: (NOTNUMS '((2) 3 S 4))
47. Trace: (NOTNUMS '(2))
48. Trace: (NOTNUMS 'NIL)
48. Trace: NOTNUMS ==> 0
47. Trace: NOTNUMS ==> 0
46. Trace: NOTNUMS ==> 0
45. Trace: NOTNUMS ==> 0
44. Trace: NOTNUMS ==> 0
This is the trace of the code. Pls help me out.
Code: Select all
(defun notnums(A)
(if (endp A)
0
(if (numberp(first A))
(notnums(rest A))
(if (listp(first A))
(notnums(first A))
(+ 1 (notnums(rest A)))
)
)
)
)
Break 52 [56]> (notnums '( 5 1 (2) 3 s 4 ))
44. Trace: (NOTNUMS '(5 1 (2) 3 S 4))
45. Trace: (NOTNUMS '(1 (2) 3 S 4))
46. Trace: (NOTNUMS '((2) 3 S 4))
47. Trace: (NOTNUMS '(2))
48. Trace: (NOTNUMS 'NIL)
48. Trace: NOTNUMS ==> 0
47. Trace: NOTNUMS ==> 0
46. Trace: NOTNUMS ==> 0
45. Trace: NOTNUMS ==> 0
44. Trace: NOTNUMS ==> 0
This is the trace of the code. Pls help me out.
Re: counting non-numeric characters.
When you reach a nested list, you need to traverse down that branch and also keep traversing the current list...
Re: counting non-numeric characters.
Hey thank you very much. Thanks to u i started to find my way around Lisp.
I also have very short question. How would u print 4 symbols without any parentheses around them. I couldn't figure out. Basically i want to print CANNOT COMPUTE DOT PRODUCT but every word has to be a symbol.
I also have very short question. How would u print 4 symbols without any parentheses around them. I couldn't figure out. Basically i want to print CANNOT COMPUTE DOT PRODUCT but every word has to be a symbol.
Re: counting non-numeric characters.
Code: Select all
(format t "~a ~a ~a ~a" 'cannot 'compute 'dot 'product)
EDIT:
Actually I've noticed I'm in homework forum so I'm adding this: It is a silly assignment.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.