# First Scheme Function, Problem with (Cond

Page 1 of 1

## 2 Replies - 1450 Views - Last Post: 09 September 2011 - 05:51 PM

### #1 jhar131

Reputation: 2
• Posts: 149
• Joined: 03-February 10

# First Scheme Function, Problem with (Cond

Posted 08 September 2011 - 07:02 PM

Hi guys. Never been on this side of the forums before. Anyway, I'm taking a class in Scheme, and I've just gotten my first assignment. It's in two parts. First, write a function that returns the total number of atoms in a given list, regardless of depth. I did this with no problems:
```(define (foo x)
(cond
((null? x) 0)
((not (list? (car x)))
(+ 1 (foo (cdr x))))
(else (+ (foo (car x)) (foo (cdr x))))))

```

Now for the second part, I'm supposed to modify the function to take a list and an atom as parameters, and return the total number of instances of that atom, also regardless of depth. Here's where I've run into trouble. What I have at the moment is:
```(define (foo x y)
(cond
((null? x) 0)
((not (list? (car x)))
(( = (car x) y)(+ 1(foo ((cdr x) y)))
((eq? (car x) y)(+ 1 (foo ((cdr x) y))))))
(else
((+ foo ((car x) y))) (foo ((cdr x) y)))))

```

Perhaps it's my "newness" to functional programming, but I cannot find the problem. When I attempt to execute a trial run, with something like:
```(foo '(1 2 3 2 1) 1)

```

I get this error message: "procedure application: expected procedure, given: (2 3 2 1); arguments were: 1" and it highlights the (( = (car x) y) line. But the error message confuses me further. It seems to me that that is what should happen, after the equivalence evaluation, it recursively passes the cdr and the atom. So.... what gives? If you find the time to look this over and give me a push in the right direction, I would greatly appreciate it. Thanks, Josh

Is This A Good Question/Topic? 0

## Replies To: First Scheme Function, Problem with (Cond

### #2 sepp2k

• D.I.C Lover

Reputation: 2579
• Posts: 4,123
• Joined: 21-June 11

## Re: First Scheme Function, Problem with (Cond

Posted 09 September 2011 - 12:34 AM

```(foo ((cdr x) y))

```

That's your error. (cdr x) evaluates to (2 3 2 1), so the above ends up as (foo ('(2 3 2 1) y)), which is asking scheme to call the "procedure" (2 3 2 1) with the argument y and then call foo with the result. But since (2 3 2 1) is a list and not a procedure, you get the error message you do.

Long story short:

```(foo (cdr x) y)

```

### #3 jhar131

Reputation: 2
• Posts: 149
• Joined: 03-February 10

## Re: First Scheme Function, Problem with (Cond

Posted 09 September 2011 - 05:51 PM

Sorry it took so long to respond (You know how school can be). First, you were correct, and thanks for the assistance. Positive rep. Secondly, there was still a logical error in the code, and while it executed, it didn't do what I wanted it to. I am posting the corrected code below for any potential future benefit someone may have. Try and note the logical difference, and what I was doing wrong with the original. Once again, thanks sepp2k, for the push.

```(define (foo x y)
(cond
((null? x) 0)
((not (list? (car x)))
(cond
((eqv? (car x) y)
(+ 1 (foo (cdr x) y)))
(else (foo (cdr x) y))))
(else (+ (foo (car x) y) (foo (cdr x) y)))))

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }