Using nil value in scheme
This permit to use nil as a value in your code
(define nil '())
12364 users tagging and storing useful source code snippets
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
(define nil '())
;;; 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
(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)
; 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))))))
; 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)))))
; 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)))))))
; 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)))))))
(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)))
(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)