wiki:Languages/Scheme

Version 6 (modified by schiptsov, 3 years ago) ( diff )

--

Scheme

Esoteric expressions

((lambda x x) (lambda x x))
(define id ((lambda (x) x) (lambda (x) x)))
((lambda xs xs))
(define list (lambda xs xs))
(define (atom? x) (not (pair? x)))

'() is a List but not a Pair

Lists

(define (list? xs)
  (cond ((null? xs) #t)
        ((and (pair? xs) (null? (cdr xs))) #t)
        (else #f)))
(define (list? xs)
  (or (null? xs) (and (pair? xs) (null? (cdr xs)))))

map

(define (map f xs)
  (if (null? xs)
      '()
      (cons (f (car xs))
            (map f (cdr xs)))))
(define (map f xs)
  (define (recur xs)
    (if (null? xs)
        '()
        (cons (f (car xs))
              (recur (cdr xs)))))
  (recur xs))
(define (map f xs)
  (letrec ((recur (lambda (xs)
                   (if (null? xs)
                       '()
                       (cons (f (car xs))
                             (recur (cdr xs)))))))
    (recur xs)))
(define (map f xs)
  (let recur ((ys xs))
    (if (null? ys)
        '()
        (cons (f (car ys))
              (recur (cdr ys))))))
(define (map f xs)
  (define (iter xs acc)
    (cond ((null? xs) (reverse acc))
          (else (iter (cdr xs) (cons (f (car xs)) acc)))))
  (iter xs '()))

folds

Newton's method


see also Standard ML

Note: See TracWiki for help on using the wiki.