Page 1 of 1

Some Idea

Posted: Sat Feb 12, 2011 10:41 am
by dsjoka
Hey,

I need some help with a program. I don't expect anyone to write it for me. I just need help with the ideas or maybe some pseudo code to help me.

Problem:

Write a Scheme function that returns the number of zeros in a given simple list of numbers.

Thanks.

Re: Some Idea

Posted: Sat Feb 12, 2011 5:41 pm
by Warren Wilkinson
In common lisp:

Code: Select all

(count 0 list)
If this is a homework assignment (and you're expected to implement count yourself) think of the problem recursively.

The number of zeros in a list is: the number of zeros in the rest of the list plus 1 (if this item is a zero) or 0 (if this item is not zero).
Also, the number of zeros in an empty list is zero.

Re: Some Idea

Posted: Tue Feb 15, 2011 8:58 am
by dsjoka
sorry I'm new to this

is this the right idea? And can you tell me if I placed the () right?

(DEFINE (countzeros list)
(IF (<>(car(list)0)
0
(+1(countzeros(cdr(list)))
))

Thanks.

Re: Some Idea

Posted: Tue Feb 15, 2011 9:48 am
by dsjoka
now that I think of this, wouldn't it be wrong.

Because if the car of the list is not equal to zero and we return 0, it will reset the count????

Re: Some Idea

Posted: Tue Feb 15, 2011 2:47 pm
by Warren Wilkinson
Yep, your right -- you've identified a problem with your implementation. Your IF statement says "If the first element of the list is NOT zero -- then the count is zero". What you probably meant to say is "If the first element of the list is NOT zero, then the count is the count-of-zeros in the rest of the list."

I see another problem involving an empty list --- trace the program mentally with an empty list, I think you'll find your program never finishes.

Re: Some Idea

Posted: Thu Feb 17, 2011 7:43 pm
by dsjoka
I have no clue whats wrong.

I get the error:
procedure application: expected procedure, given: 0 (no arguments)
code is:

Code: Select all

(define (countzeros list)
  (cond
    ((null? car list) 0)
    ((= (car list) 0 )) (+ 1 (countzeros(cdr list)))
    (else (countzeros(cdr list)))))
I tried (countzeros (0))
Which should return 1

Is my logic even correct? If so am I missing (), or did I put a + sign or = sign in the wrong place?

This is suppose to output the number of zeros is a number list.

Re: Some Idea

Posted: Fri Feb 18, 2011 7:51 am
by gugamilare
You've forgotten one pair of parenthesis:

Code: Select all

(define (countzeros list)
  (cond
    ((null? (car list)) 0)
    ((= (car list) 0 )) (+ 1 (countzeros(cdr list)))
    (else (countzeros(cdr list)))))
Also, you should call (countzeros '(0)) or (countzeros (list 0)), not (countzeros (0)).

Re: Some Idea

Posted: Fri Feb 18, 2011 8:16 am
by dsjoka
ok so when I do that it only returns true and not how many zeros there is?
> (countzeros '(0))
#t
> (countzeros (list 0))
#t
> (countzeros '(0 0))
#t
>

Re: Some Idea

Posted: Fri Feb 18, 2011 10:13 am
by gugamilare
Sorry, there is one parenthesis misplaced. You should also change the first condition - you don't want to test if the first element of the list is null, but rather if the list is null.

Code: Select all

(define (countzeros list)
  (cond
    ((null? list) 0)
    ((= (car list) 0 ) (+ 1 (countzeros (cdr list))))
    (else (countzeros (cdr list)))))