## Some Idea

Discussion of Scheme and Racket

### Some Idea

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

Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

### Re: Some Idea

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.
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

Warren Wilkinson

Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta

### Re: Some Idea

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

Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

### Re: Some Idea

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????
dsjoka

Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

### Re: Some Idea

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.
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

Warren Wilkinson

Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta

### Re: Some Idea

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

Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

### Re: Some Idea

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

Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

### Re: Some Idea

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

Posts: 14
Joined: Thu Feb 10, 2011 1:30 pm

### Re: Some Idea

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)))))`
gugamilare

Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil