I know... I need to use that list of 1's and 0's as a binary number so that I
can convert it to decimal... but I don't know how to do it because I've never
used Lisp
I'm working at it though
I tried writing this but it gave me errors that I don't know how to fix...
(binnum) is supposed to be the list
>(setf num 0)
0
>(setf ex 0)
0
>(defun s-todec (binnum)
(if (null '(binnum))
nil
(setf num (+ num (* (last '(binnum)) (expt 2 ex))));;num=num+last
digit*2^
ex
(setf ex (+ ex 1)) ;;exponent++
(s-todec (butlast '(binnum)))))
S-TODEC
>(S-TODEC '(1 0 1 1))
Error: Too many arguments.
Fast links are on: do (si::use-fast-links nil) for debugging
Error signalled by IF.
Broken at IF. Type :H for Help.
>>
***********************************
www.holly-cam.com
In article <·····························@ng-cl1.aol.com>,
holly-cam.com <········@aol.comnospam> wrote:
>>(defun s-todec (binnum)
>
> (if (null '(binnum))
> nil
> (setf num (+ num (* (last '(binnum)) (expt 2 ex))));;num=num+last
>digit*2^
>ex
> (setf ex (+ ex 1)) ;;exponent++
> (s-todec (butlast '(binnum)))))
>
>S-TODEC
>
>>(S-TODEC '(1 0 1 1))
>
>Error: Too many arguments.
>Fast links are on: do (si::use-fast-links nil) for debugging
>Error signalled by IF.
>Broken at IF. Type :H for Help.
You have 5 arguments to IF. Like I wrote in an earlier message, IF takes 2
or 3 arguments. It's either:
(if <condition>
<true-expression>)
or:
(if <condition>
<true-expression>
<false-expression>)
In your code, <condition> is (null '(binnum)), <true-expression> is nil,
<false-expression> is (setf num (+ num (* (last '(binnum)) (expt 2 ex)))),
but (setf ex (+ ex 1)) and (s-todec (butlast '(binnum))) don't fit anywhere
in the pattern.
If you want to do multiple things in a single expression, you must use
PROGN:
(if (null '(binnum))
nil
(progn (setf num (+ num (* (last '(binnum)) (expt 2 ex))));;num=num+last digit*2^ ex
(setf ex (+ ex 1)) ;;exponent++
(s-todec (butlast '(binnum)))))
Your comments suggest that you're familiar with C. You can think of PROGN
as being like C's { ... }, which are necessary when you want to use a block
of statements as the then-clause or else-clause of an "if" statement.
Common Lisp has a number of places where you don't need to use PROGN
explicitly because it's unambiguous, such as the body of a function (these
are referred to as "implicit PROGNs"), but IF requires it.
--
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.