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-4 of 4 total  RSS 

binarysearch.scm

// Binary Search

   1  
   2  ; Andrew Pennebaker
   3  ; 18 Feb 2007
   4  ; License: GPL
   5  ; URL: http://snippets.dzone.com/posts/show/3535
   6  
   7  (define binary-search
   8  	(lambda (ls value low high)
   9  		(let ((mid (floor (/ (+ low high) 2))))
  10  			(cond
  11  				((> low high) -1)
  12  				((= (list-ref ls mid) value) mid)
  13  				((> (list-ref ls mid) value) (binary-search ls value low (- mid 1)))
  14  				((< (list-ref ls mid) value) (binary-search ls value (+ mid 1) high))))))

hanoi.scm

// Solves the Towers of Hanoi puzzle.

   1  
   2  ; Andrew Pennebaker
   3  ; 9 Feb 2007
   4  ; License: GPL
   5  ; URL: http://snippets.dzone.com/posts/show/3492
   6  
   7  (define *start* 0)
   8  (define *aux* 1)
   9  (define *end* 2)
  10  
  11  (define hanoi
  12  	(lambda (n start aux end)
  13  		(if (= n 1)
  14  			(list start end)
  15  			(append
  16  				(hanoi (- n 1) start aux end)
  17  				(list start aux)
  18  				(hanoi (- n 1) aux start end)))))

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-4 of 4 total  RSS