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

« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS 

Using nil value in scheme

// description of your code here
This permit to use nil as a value in your code

(define nil '())

mapi: similar to standard map in Scheme, but function has index of elements as second parameter.


;;;  mapi: similar to standard map in Scheme, but function has index of elements as parameter.
;;;
(define (mapi p l)
  (let loop ((l l) (i 0) (r '()))
    (if (pair? l)
	(loop (cdr l) (+ i 1) (cons (p (car l) i) r))
	(reverse r))))

;; Example: converting a binary number given as a list of binary digits. 
;; (apply + (mapi (lambda (x i) (* x (expt 2 i))) '(0 1 1)))
;; => 6

Square root in Scheme

This implements a square root function in Scheme using Newton's approximation algorithm.

Basically, if you want the square root of y and your best guess is x, a better guess is the average of x and y/x.

So, if you want the square root of 10 and your best guess is 3,
x=3 y=10 x<-(3 + 10/3)/2
x=3.166 y=10 x<-(3.166 + 10/3.133)/2
x=3.162
...

(define (sqrt-iter guess x)
    (if (good-enough? guess x)
        guess
        (sqrt-iter (improve guess x)
                    x)))

(define (improve guess x)
    (average guess (/ x guess)))

(define (average x y)
    (/ (+ x y) 2))

(define (good-enough? guess x)
    (< (abs (- (square guess) x))
        0.001))

(define (sqrt x)
    (sqrt-iter 1.0 x))

(sqrt 10)


Hackish way to get a list of module exports under PLT

(define (get-provided-names-from-a-module path)
(syntax-property (expand (with-input-from-file (path->string path) read))
'module-variable-provides))

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)))))))

car cdr cons from little LISPer

// description of your code here

(define cons
 (lambda (u v)
   (lambda (b)
     (cond (b u)
     (#t v)))))
(define lunch (cons 'apple '()))
(define car (lambda (l) (l #t)))
(define cdr (lambda (l) (l #f)))


the applicative-order Y-combinator

// description of your code here

(define Y
  (lambda (f)
    (let ((future
            (lambda (future)
              (f (lambda (arg) 
                   ((future future) arg))))))
      (future future))))

((Y (lambda (factorial)
      (lambda (n)
        (if (= n 0)
            1
            (* n (factorial (- n 1)))))))
 42)
« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS