PDA

View Full Version : How do you write a clisp program that counts the number of 0's in a number?(reps)



bartosh
04-23-2014, 03:46 PM
My book only explains scheme but not common lisp. I'm looking online and the closest directions i could come up with are:

(defun (count-zeroes lst)
(fold (lambda (elem count)
(if (zero? elem) (+ count 1) count))
0 lst))

It doesnt work. Can anybody explain to me how to write a program that counts 0's in a list of numbers> Or link me a decent website explains how to do this specific type of function?

niktak11
04-23-2014, 04:02 PM
Idk wtf that language it is but I would convert it to a string and then go through each character and compare it to '0'

If there isn't any type casting then check to see if the number mod 10 equals zero (meaning that the ones digit is zero). Then subtract num mod 10 from num and divide by 10 to get rid of the ones digit and repeat the process for each digit in the original number

Edit: just realized that you are counting zeros in a list and not in a single long number. I don't know what that crazy syntax is but you just need the equivalent of a for loop to go through the list and for each iteration compare an element in the list to zero with an if (which looks sorta like what you're doing)

bartosh
04-23-2014, 04:17 PM
I mean it's easy to do in java or c++ but common lisp is ridiculous. A upper division programming languages class is...interesting.

But i got it.
(defun my-count (a L)
(cond((null L) 0)
((equal a (car L)) (+ 1 my-count a (crd L))))
(t (my-count a (crd L)))))

MiscMathematician
04-23-2014, 05:02 PM
I mean it's easy to do in java or c++ but common lisp is ridiculous. A upper division programming languages class is...interesting.

But i got it.
(defun my-count (a L)
(cond((null L) 0)
((equal a (car L)) (+ 1 my-count a (crd L))))
(t (my-count a (crd L)))))

did someone with down syndrome create this language?

NonSmoker
04-23-2014, 05:48 PM
What a schtewpid language