Posted by bluestorm Fri 23rd Feb 2007 18:45 - Syntax is Scheme - 54 views
Download | New Post | Modify | Hide line numbers
Download | New Post | Modify | Hide line numbers
Description:
scheme implementation of the ocaml standard List library
scheme implementation of the ocaml standard List library
-
(define (length list)
-
(if (null? list) 0
-
(+ 1 (length (cdr list)))))
-
-
(define (nth list n)
-
(if (= n 0) (car list)
-
(nth (cdr list) (- n 1))))
-
-
(define (reverse list)
-
(define (rev acc list)
-
(if (null? list) acc
-
(rev (cons (car list) acc) (cdr list))))
-
(rev () list))
-
-
(define (append a b)
-
(if (null? a) b
-
(cons (car a) (append (cdr a) b))))
-
-
(define (rev_append a b)
-
(if (null? a) b
-
(append (cdr a) (cons (car a) b))))
-
-
(define (concat list)
-
(if (null? list) ()
-
(append (car list) (concat (cdr list)))))
-
-
(define flatten concat)
-
-
(define (iter f list)
-
(if (null? list) #t
-
(begin
-
(f (car list))
-
(iter f (cdr list)))))
-
-
(define (map f list)
-
(if (null? list) ()
-
(cons (f (car list)) (map f (cdr list)))))
-
-
(define (rev_map f list)
-
(define (aux_map acc list)
-
(if (null? list) acc
-
(aux_map (cons (f (car list)) acc) (cdr list))))
-
(aux_map () list))
-
-
(define (fold_left f acc list)
-
(if (null? list) acc
-
(fold_left f (f acc (car list)) (cdr list))))
-
-
(define (fold_right f list acc)
-
(if (null? list) acc
-
(f (car list) (fold_right f (cdr list) acc))))
-
-
(define (for_all p list)
-
(if (null? list) #t
-
(if (not (p (car list))) #f
-
(for_all p (cdr list)))))
-
-
(define (exists p list)
-
(not (for_all (lambda (x) (not (p x))) list)))
-
-
(define (mem elem list)
-
(exists (lambda (x) (= elem x)) list))
-
-
(define (find p list)
-
(if (null? list) #f
-
(if (p (car list)) (car list)
-
(find p (cdr list)))))
-
-
(define (find_all p list)
-
(define (add list x)
-
(if (p x) (cons x list) list))
-
(reverse (fold_left add () list)))
-
-
(define filter find_all)
-
-
(define (partition p list)
-
(if (null? list) '(())
-
(let* ((hd (car list))
-
(tl (cdr list))
-
(acc (partition p tl)))
-
(if (p hd)
-
(cons (cons hd (car acc)) (cdr acc))
-
(cons (car acc) (cons hd (cdr acc)))))))
-
-
(define (assoc key list)
-
(car (find (lambda (elem) (= key (car elem))) list)))
-
-
(define (mem_assoc key list)
-
(exists (lambda (elem) (= key (car elem))) list))
-
-
(define (remove_assoc key list)
-
(filter (lambda (elem) (not (= key (car elem)))) list))
-
-
(define (split list)
-
(define (consplit elem acc)
-
(cons
-
(cons (car elem) (car acc))
-
(cons (cadr elem) (cdr acc))))
-
(fold_right consplit list '(())))
-
-
PermaLink to this entry https://pastebin.co.uk/10851
Posted by bluestorm Fri 23rd Feb 2007 18:45 - Syntax is Scheme - 54 views
Download | New Post | Modify | Hide line numbers
Download | New Post | Modify | Hide line numbers
Comments: 0