All pastes #2093610 Raw Edit

display-list-indent.scm

public text v1 · immutable
#2093610 ·published 2011-11-10 16:48 UTC
rendered paste body
;;;;; Filename: display-list-indent.scm
;;;;;; Description: Display Lists in Indented Form with Bracketing
;;;;;;; Language: Scheme
;;;;;;;; Author: Matthew Lewis (matt@mplewis.com)
;;;;;;;;; Date: Nov 10, 2011
;;;;;;;;;; Version: 1.0
;
; This is a Scheme function to display lists in a human-readable form. When passed a list as an argument, it returns the list in a bracketed and indented form.
;
; The program should function with any list Scheme handles.
; If an item in a list is two items, not two items and an empty set [example: (2 . 4), which car/cdrs out to 2 and 4, vs (2 4), which car/cdrs out to 2 and 4 and ()], a dot will be printed in between the two to indicate there is no container.
;
; Currently the only way to change the level of indentation per each level of the list is to change the number of spaces inside the (indent-helper) function.
; Example: change (display "    ") in line 5 of the function to (display "  ") to change indentation to 2 spaces.
; This can probably be made easier to change with a (let) function.
;
; The main function is run as follows:
;     (displist list)
; where [list] is any Scheme list.
;
; Sample input:
;     (displist '(((3 . 4)(6 8 12))(2 13 (4 . 1))))
;
; Sample output:
; (
;     (
;         (
;             3
;             .
;             4
;         )
;         (
;             6
;             8
;         (
;             12
;         )
;     )
;     (
;         2
;         13
;     (
;         (
;             4
;             .
;             1
;         )
;     )
; )

(define (displist origlist)
    (define (indent-helper n)
        (cond
            ((> n 0)
                (display "    ")
                (indent-helper (- n 1)))
            (else
                (display ""))))
    (define (dspl-help templist depth bracketed? amicdr?)
        (cond
            ((null? templist)
                (indent-helper depth)
                (display ")")(newline))
            ((pair? templist)
                (cond 
                    ((not bracketed?)
                        (indent-helper depth)
                        (display "(")(newline)
                        (dspl-help (car templist) (+ depth 1) #f #f)
                        (dspl-help (cdr templist) depth #t #t))
                    (else
                        (dspl-help (car templist) (+ depth 1) #f #f)
                        (dspl-help (cdr templist) depth #f #t))))
            (else
                (cond
                    (amicdr?
                        (indent-helper (+ depth 1))
                        (display ".")(newline)
                        (dspl-help (list templist) depth bracketed? #f))
                    (else
                        (indent-helper depth)
                        (display templist)(newline))))))
    (dspl-help origlist 0 #f #f))