hello everyone. i am new to Lisp. i dont know how to perform binary operations in LISP?
can anyone share sample of code for binary addition or multiplication plz
Binary operations in Lisp
Re: Binary operations in Lisp
A binary operation is treated as any other n-ary operation. There is nothing special about it. Use prefix notation:
Code: Select all
(+ 1 2) ; = 3
(* 2 3) ; = 6
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
Re: Binary operations in Lisp
Integers don't have base when they are stored. Your reader can read numbers in several different bases.
Now when makeing a string out of the number you get to choose the base:
There you have it. 30 displayed in 4 different bases. In CL you have the special variable *print-base* which sets the default base in which numbers are printed and *read-base* which sets the default base in which numbers are read when parsed:
Thats about all there is to know about displaying and parsing numbers. Now all your base are belong to you
Code: Select all
(+ 10 #xa #b1010) ; ==> 30
Code: Select all
(let ((thirty (+ 10 #xa #b1010)))
(format nil "~n ~x ~o ~b" thirty thirty thirty thirty))
; ==> "30 1E 36 11110"
Code: Select all
(let ((*print-base* 16)
(*read-base* 2))
(princ (read-from-string "1011"))) ; ==> B
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
Currently I'm planning a Scheme compiler :p
Re: Binary operations in Lisp
I think the question was about binary operations (operations that take exactly two arguments), not how to compute numbers represented in binary format. And the question smells very much like homework...