Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andrew Pennebaker http://mcandre.devjavu.com/wiki

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

bindec.scm

// Convert list of 1s and 0s back to base ten number.

   1  
   2  ; Andrew Pennebaker
   3  ; 5 Feb 2007
   4  ; License: GPL
   5  ; URL: http://snippets.dzone.com/posts/show/3479
   6  
   7  (define bin->dec
   8  	(lambda (b)
   9  		(cond
  10  			((integer? b) b)
  11  			((= (length b) 0) 0)
  12  			(else
  13  				(+
  14  					(* (expt 2 (- (length b) 1)) (car b))
  15  					(bin->dec (cdr b)))))))

decbin.scm

// Converts a base ten integer to a list of 1s and 0s

   1  
   2  ; Andrew Pennebaker
   3  ; 3 Feb 2007
   4  ; License: GPL
   5  ; URL: http://snippets.dzone.com/posts/show/3478
   6  
   7  (define dec->bin
   8  	(lambda (d)
   9  		(cond
  10  			((< d 1) (list 0))
  11  			((= d 1) (list 1))
  12  			((> d 1) (append
  13  				(dec->bin (floor (/ d 2)))
  14  				(list (if (= (modulo d 2) 0) 0 1)))))))
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS