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

; Andrew Pennebaker
; 18 Feb 2007
; License: GPL
; URL: http://snippets.dzone.com/posts/show/3535

(define binary-search
	(lambda (ls value low high)
		(let ((mid (floor (/ (+ low high) 2))))
			(cond
				((> low high) -1)
				((= (list-ref ls mid) value) mid)
				((> (list-ref ls mid) value) (binary-search ls value low (- mid 1)))
				((< (list-ref ls mid) value) (binary-search ls value (+ mid 1) high))))))

hanoi.scm

// Solves the Towers of Hanoi puzzle.

; Andrew Pennebaker
; 9 Feb 2007
; License: GPL
; URL: http://snippets.dzone.com/posts/show/3492

(define *start* 0)
(define *aux* 1)
(define *end* 2)

(define hanoi
	(lambda (n start aux end)
		(if (= n 1)
			(list start end)
			(append
				(hanoi (- n 1) start aux end)
				(list start aux)
				(hanoi (- n 1) aux start end)))))

bindec.scm

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

; Andrew Pennebaker
; 5 Feb 2007
; License: GPL
; URL: http://snippets.dzone.com/posts/show/3479

(define bin->dec
	(lambda (b)
		(cond
			((integer? b) b)
			((= (length b) 0) 0)
			(else
				(+
					(* (expt 2 (- (length b) 1)) (car b))
					(bin->dec (cdr b)))))))

decbin.scm

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

; Andrew Pennebaker
; 3 Feb 2007
; License: GPL
; URL: http://snippets.dzone.com/posts/show/3478

(define dec->bin
	(lambda (d)
		(cond
			((< d 1) (list 0))
			((= d 1) (list 1))
			((> d 1) (append
				(dec->bin (floor (/ d 2)))
				(list (if (= (modulo d 2) 0) 0 1)))))))
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS