decimal to binary converter code in lisp

907 ogledov
Preskoči na prvo neprebrano sporočilo

Vishakha Jadhav

neprebran,
20. jan. 2015, 23:28:5320. 1. 15
do land-o...@googlegroups.com
plz give me
decimal to binary converter code in lisp

Mauricio

neprebran,
22. jan. 2015, 10:12:2722. 1. 15
do 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

neprebran,
22. jan. 2015, 11:11:0922. 1. 15
do 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

neprebran,
23. jan. 2015, 20:42:0123. 1. 15
do 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

neprebran,
25. feb. 2017, 07:48:3625. 2. 17
do 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

neprebran,
25. feb. 2017, 09:36:0225. 2. 17
do 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.
Odgovori vsem
Odgovori avtorju
Posreduj
0 novih sporočil