List Processing in Scheme

List is the fundamental element in Scheme1).

Contains?

;;contains? : number list-of-number -> Boolran
;;to determine whether elmt is contained in the lst
;;example (contains? 3 (cons 1 (cons 3 empty))) => true
(define (contains? elmt lst)
  (cond
    [(empty? lst)
     false]
    [else
     (cond
       [(= elmt (first lst)) ;if first of lst is elmt, return true
        true]
       [else ;else, do the same with (rest lst) until lst is empty
        (contains? elmt (rest lst))])]))

Substitute

codereveal hidden content

Remove

Remove single or duplicated codereveal hidden content

Numbers as lists of digits

;;list-digits : number -> list-of-numbers
;;to list a natural number's digits with one's digits at first of list
;;example : (list-digits 9) => (list 9)
;;          (list-digits 10) => (list 0 1)
(define (list-digits n)
  (cond
    [(< n 10) (cons n empty)]
    [else
     (cons
      (remainder n 10)
      (list-digits (quotient n 10)))]))

Multi Line (Table)

codereveal hidden content

Day in the year (Another version)

codereveal hidden content

1) for the reason that scheme is based on Lisp which stands for LISt Processing
 
sci/cs/scheme/list.txt · Last modified: 2006/06/22 01:02 by flanker27
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki