counting non-numeric characters.

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
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.

counting non-numeric characters.

Postby djtlg » Fri Sep 27, 2013 1:45 pm

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.
djtlg
 
Posts: 3
Joined: Fri Sep 27, 2013 1:37 pm

Re: counting non-numeric characters.

Postby nuntius » Fri Sep 27, 2013 6:48 pm

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...
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: counting non-numeric characters.

Postby djtlg » Sat Sep 28, 2013 12:17 am

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.
djtlg
 
Posts: 3
Joined: Fri Sep 27, 2013 1:37 pm

Re: counting non-numeric characters.

Postby nuntius » Sat Sep 28, 2013 6:57 pm

When you reach a nested list, you need to traverse down that branch and also keep traversing the current list...
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: counting non-numeric characters.

Postby djtlg » Sat Sep 28, 2013 9:37 pm

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.
djtlg
 
Posts: 3
Joined: Fri Sep 27, 2013 1:37 pm

Re: counting non-numeric characters.

Postby Goheeca » Sun Sep 29, 2013 2:27 pm

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


Return to Homework

Who is online

Users browsing this forum: No registered users and 1 guest

cron