Posted by bluestorm Fri 23rd Feb 2007 18:45 - Syntax is Scheme - 54 views
Download | New Post | Modify | Hide line numbers
Description:
scheme implementation of the ocaml standard List library

  1. (define (length list)
  2.   (if (null? list) 0
  3.     (+ 1 (length (cdr list)))))
  4.  
  5. (define (nth list n)
  6.   (if (= n 0) (car list)
  7.     (nth (cdr list) (- n 1))))
  8.  
  9. (define (reverse list)
  10.   (define (rev acc list)
  11.     (if (null? list) acc
  12.       (rev (cons (car list) acc) (cdr list))))
  13.   (rev () list))
  14.  
  15. (define (append a b)
  16.   (if (null? a) b
  17.     (cons (car a) (append (cdr a) b))))
  18.  
  19. (define (rev_append a b)
  20.   (if (null? a) b
  21.     (append (cdr a) (cons (car a) b))))
  22.  
  23. (define (concat list)
  24.   (if (null? list) ()
  25.     (append (car list) (concat (cdr list)))))
  26.  
  27. (define flatten concat)
  28.  
  29. (define (iter f list)
  30.   (if (null? list) #t
  31.     (begin
  32.      (f (car list))
  33.      (iter f (cdr list)))))   
  34.  
  35. (define (map f list)
  36.   (if (null? list) ()
  37.     (cons (f (car list)) (map f (cdr list)))))
  38.  
  39. (define (rev_map f list)
  40.   (define (aux_map acc list)
  41.     (if (null? list) acc
  42.       (aux_map (cons (f (car list)) acc) (cdr list))))
  43.   (aux_map () list))
  44.  
  45. (define (fold_left f acc list)
  46.   (if (null? list) acc
  47.     (fold_left f (f acc (car list)) (cdr list))))
  48.  
  49. (define (fold_right f list acc)
  50.   (if (null? list) acc
  51.     (f (car list) (fold_right f (cdr list) acc))))
  52.  
  53. (define (for_all p list)
  54.   (if (null? list) #t
  55.     (if (not (p (car list))) #f
  56.     (for_all p (cdr list)))))
  57.  
  58. (define (exists p list)
  59.   (not (for_all (lambda (x) (not (p x))) list)))
  60.  
  61. (define (mem elem list)
  62.   (exists (lambda (x) (= elem x)) list))
  63.  
  64. (define (find p list)
  65.   (if (null? list) #f
  66.     (if (p (car list)) (car list)
  67.       (find p (cdr list)))))
  68.  
  69. (define (find_all p list)
  70.   (define (add list x)
  71.     (if (p x) (cons x list) list))
  72.   (reverse (fold_left add () list)))
  73.  
  74. (define filter find_all)
  75.  
  76. (define (partition p list)
  77.   (if (null? list) '(())
  78.     (let* ((hd (car list))
  79.        (tl (cdr list))
  80.        (acc (partition p tl)))
  81.       (if (p hd)
  82.       (cons (cons hd (car acc)) (cdr acc))
  83.       (cons (car acc) (cons hd (cdr acc)))))))
  84.  
  85. (define (assoc key list)
  86.   (car (find (lambda (elem) (= key (car elem))) list)))
  87.  
  88. (define (mem_assoc key list)
  89.   (exists (lambda (elem) (= key (car elem))) list))
  90.  
  91. (define (remove_assoc key list)
  92.   (filter (lambda (elem) (not (= key (car elem)))) list))
  93.  
  94. (define (split list)
  95.   (define (consplit elem acc)
  96.     (cons
  97.      (cons (car elem) (car acc))
  98.      (cons (cadr elem) (cdr acc))))
  99.   (fold_right consplit list '(())))
  100.  
  101.  

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

 

Comments: 0