Page 1 of 1

counting non-numeric characters.

Posted: Fri Sep 27, 2013 1:45 pm
by djtlg
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.

Re: counting non-numeric characters.

Posted: Fri Sep 27, 2013 6:48 pm
by nuntius
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...

Re: counting non-numeric characters.

Posted: Sat Sep 28, 2013 12:17 am
by djtlg
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.

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)))
			)
		)
	)
)
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.

Re: counting non-numeric characters.

Posted: Sat Sep 28, 2013 6:57 pm
by nuntius
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.

Posted: Sat Sep 28, 2013 9:37 pm
by djtlg
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.

Re: counting non-numeric characters.

Posted: Sun Sep 29, 2013 2:27 pm
by Goheeca

Code: Select all

(format t "~a ~a ~a ~a" 'cannot 'compute 'dot 'product)
It's really weird you need it to be symbols. The better would be perhaps use separated strings.
EDIT:
Actually I've noticed I'm in homework forum so I'm adding this: It is a silly assignment.