Announcements and discussions for Gnus, the GNU Emacs Usenet newsreader
 help / color / mirror / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: info-gnus-english@gnu.org
Subject: Re: Strange results
Date: Sat, 07 Sep 2013 14:32:02 +0200	[thread overview]
Message-ID: <87r4d0dd8d.fsf@informatimago.com> (raw)
In-Reply-To: <73ac625b-72a4-4719-9ca4-e4d5da9e8c0a@googlegroups.com>

Dwaddle <rmborchers@gmail.com> writes:

> Hi
>
> i've been dabbling around in elisp for a while. Made this function
>
> The issue is the byte var which 'remembers' the result from a previous calculation.
>
> (integer-bin 5)     [0 0 0 0 0 1 0 1]
> (integer-bin 3)     [0 0 0 0 0 1 1 1]
>
> To beat the critics I know it isn't very clean programmed and a loop would make it al lot shorter

Yes, use a loop!


> <code>
> (defun integer-bin (dec)
>   "Convert integer to binairy"
>   (setq 8bit [128 64 32 16 8 4 2 1])
>   (setq byte [0 0 0 0 0 0 0 0])


Instead of setq, use let to introduce local variables.


[0 0 0 0 0 0 0 0] is a literal "constant" vector.

(vector 0 0 0 0 0 0 0 0) produces a new mutable vector.

(make-vector 8 0) produces a new mutable vector with each slot
                  initialized to the same object..



(require 'cl)
         
(defun* integer-bin (integer &optional (word-size 8))
   "Convert `integer' to binary"
   (let ((word (make-vector word-size 0))) ; a new mutable vector
      (loop
        for bit-index from (1- word-size) downto 0
        for current = integer then (truncate current 2) ; or (ash current -1)
        for bit = (mod current 2)                       ; or (logand current 1)
         do (setf (aref word  bit-index) bit))
      word))

(integer-bin 42)     ; --> [0 0 1 0 1 0 1 0]
(integer-bin 42 16)  ; --> [0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0]


(defun integer-to-base (integer base word-size)
   "Convert `integer' to base `base', over `word-size` digits."
   (let ((word (make-vector word-size 0))) ; a new mutable vector
      (loop
        for digit-index from (1- word-size) downto 0
        for current = integer then (truncate current base)
        for digit = (mod current base)
         do (setf (aref word  digit-index) digit))
      word))

(integer-to-base 42 2 8)  ; --> [0 0 1 0 1 0 1 0]
(integer-to-base 42 8 8)  ; --> [0 0 0 0 0 0 5 2]
(integer-to-base 42 24 4) ; --> [0 0 1 18]

-- 
__Pascal Bourguignon__
http://www.informatimago.com/

      reply	other threads:[~2013-09-07 12:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-07  6:54 Dwaddle
2013-09-07 12:32 ` Pascal J. Bourguignon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r4d0dd8d.fsf@informatimago.com \
    --to=pjb@informatimago.com \
    --cc=info-gnus-english@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).