From: David Nixon
Subject: Help with small prog please
Date: 
Message-ID: <76imj1$sa2$1@news4.svr.pol.co.uk>
(defun count-odd (s)

(cond ((null s) 0)
      ((atom s) 1)
      (t (apply '+ (mapcar 'count-odd s)))))
                 (count-odd '(5 56 7 8))
I am trying to count the odd numbers but cannot find what I am doing wrong
Can anyone please help.
Also :-
(defun wall (n)
  (cond ((= n 0) nil  (oddp n))

         (t (cons 'brick (wall (- n 1)))))))

               (wall 7)

This program is supposed to construct a wall brick mortar brick mortar etc,
but it must always start with brick. What am I doing wrong
Any help is greatly appreciated.
Dave

From: Stig Hemmer
Subject: Re: Help with small prog please
Date: 
Message-ID: <ekviuerx8ae.fsf@gnoll.pvv.ntnu.no>
"David Nixon" <·····@nixon25.freeserve.co.uk> writes:

> (defun count-odd (s)
>    (cond ((null s) 0)
>          ((atom s) 1)
>          (t (apply '+ (mapcar 'count-odd s)))))

> (count-odd '(5 56 7 8))
[gives 4]

> I am trying to count the odd numbers but cannot find what I am doing wrong

What you have here is a function that counts atoms.  There is no test
for the oddness of the things you are counting.

Try changing that 1 up there to something more complicated involving
(oddp s).

> Also :-
> (defun wall (n)
>    (cond ((= n 0) nil  (oddp n))
>          (t (cons 'brick (wall (- n 1)))))))
> 
> (wall 7)
[gives (BRICK BRICK BRICK BRICK BRICK BRICK BRICK)]

> This program is supposed to construct a wall brick mortar brick mortar etc,
> but it must always start with brick. What am I doing wrong

Well, for one thing, your program has no mortar in it...

Also, the first cond clause has three parts, which is not very useful.
As it stands it will only call (oddp n) when it already knows that 
(= n 0), so the result is always nil.

There is an unmatched right parenthesis at the end of your function.
Does this mean that you are working with a Lisp system that doesn't
help you with indenting and matching parentheses?

If so, my condolences.  Lisp becomes _much_ easier with a bit of
support from the computer.

Hope this helps.

Stig Hemmer,
Jack of a Few Trades.
From: Steve Gonedes
Subject: Re: Help with small prog please
Date: 
Message-ID: <m290fmdf5d.fsf@KludgeUnix.com>
"David Nixon" <·····@nixon25.freeserve.co.uk> writes:

<
< (defun count-odd (s)
<
< (cond ((null s) 0)
<       ((atom s) 1)
<       (t (apply '+ (mapcar 'count-odd s)))))
<                  (count-odd '(5 56 7 8))
<
< I am trying to count the odd numbers but cannot find what I am doing
< wrong Can anyone please help.

You can use the function `count-if'.

(count-if #'oddp '(5 56 7 8)) => 2

You could use loop.

(loop for number in '(5 56 7 8)
   count (oddp number))

=> 2

(defun count-odd (s)
 (cond
  ((null s) 0)   ; if null return 0
  ((listp s)     ; if a list, call count-odd on first and rest summing result.
    (+ (count-odd (first s))
       (count-odd (rest s))))
  ((oddp s) 1)   ; if s is not a list, or empty, and is odd, return 1.
  (t 0)))        ; anything else, return 0.

(count-odd '(5 56 7 8)) => 2

< Also :-
< (defun wall (n)
<   (cond ((= n 0) nil  (oddp n))
<
<          (t (cons 'brick (wall (- n 1)))))))
<
<                (wall 7)

(defun wall (brick-count)
  (cond ((zerop brick-count) (list 'brick))
        (t (cons 'brick
             (cons 'mortar
                (wall (- brick-count 1)))))))

(wall 3) => (brick mortar brick mortar brick mortar brick) ?

The trick (I think) is to return a brick instead of nil? You could
also use evenp and oddp to solve the problem (I think this is probably
what you wanted?).

You can use loop for this one too.

(loop for brick-count from 0 upto 5
  if (oddp brick-count)
     collect 'brick into wall
  else collect 'mortar into wall
  finally (return (reverse wall)))
From: CsO
Subject: Re: Help with small prog please
Date: 
Message-ID: <76kbat$69c$1@news5.svr.pol.co.uk>
David Nixon wrote in message <············@news4.svr.pol.co.uk>...
>(defun count-odd (s)
>  (cond ((null s) 0)
>      ((atom s) 1)
>      (t (apply '+ (mapcar 'count-odd s)))))
>                 (count-odd '(5 56 7 8))
>I am trying to count the odd numbers but cannot find what I am doing wrong
>Can anyone please help.
(defun count-odd (s)
   (if (atom s)
      (if (numberp s) (if (oddp s) 1 0) 0)
      (apply '+ (mapcar 'count-odd s))))