decimal to binary converter code in lisp

811 views
Skip to first unread message

Vishakha Jadhav

unread,
Jan 20, 2015, 11:28:53 PM1/20/15
to land-o...@googlegroups.com
plz give me
decimal to binary converter code in lisp

Mauricio

unread,
Jan 22, 2015, 10:12:27 AM1/22/15
to land-o...@googlegroups.com
There are several ways to do this, and some may be more appropriate to what you are attempting to use it for, but here is a recursive approach I used for certain purposes.

(defun dec->bin (n) 
  "Converts a number to a list contaning its binary representation,
   LSB comes first"
  (multiple-value-bind (r m) (floor n 2) 
    (if (= n 0) nil (cons m (dec->bin r)))))

Vishakha Jadhav

unread,
Jan 22, 2015, 11:11:09 AM1/22/15
to land-o...@googlegroups.com
thanks

--
You received this message because you are subscribed to a topic in the Google Groups "Land of Lisp" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/land-of-lisp/2jhW5wymksc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to land-of-lisp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John de la Garza

unread,
Jan 23, 2015, 8:42:01 PM1/23/15
to land-o...@googlegroups.com
On Thu, Jan 22, 2015 at 07:12:27AM -0800, Mauricio wrote:
> (defun dec->bin (n)
> "Converts a number to a list contaning its binary representation,
> LSB comes first"
> (multiple-value-bind (r m) (floor n 2)
> (if (= n 0) nil (cons m (dec->bin r)))))
>

nice

Pratik Doshi

unread,
Feb 25, 2017, 7:48:36 AM2/25/17
to Land of Lisp
Can you please let me know how to take input from user.

 I am trying to write code I want to write a function btod to turn a binary number represented as a list of digits into the equivalent decimal number, using reduce and lambda. For example, given the list (1 1 0 1) it should return the decimal number 13. 

MY ANSWER IS:
(reduce (lambda (x y) (+ (* x 2) y)) '(1 1 0 1))

but instead of defining values (1 1 0 1)  I want to take value from user. Please help

Edward Kenworthy

unread,
Feb 25, 2017, 9:36:02 AM2/25/17
to Pratik Doshi, Land of Lisp

 

Two main ways, make it a web application (have a look at Hunchentoot and similar). Or from the command line like this from Practical Common Lisp:

 

(defun prompt-read (prompt)

  (format *query-io* "~a: " prompt)

  (force-output *query-io*)

  (read-line *query-io*))

 

 

Also see Chapter 6 of Land of Lisp.

 

Edward

 

P.S.

 

Just as a style note, use binary-to-decimal or binary->decimal rather than btod.

 

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to the Google Groups "Land of Lisp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to land-of-lisp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages