add list_print.scm
This commit is contained in:
parent
f65ea2ae5d
commit
729662be8e
1 changed files with 40 additions and 0 deletions
40
misc/list_print.scm
Normal file
40
misc/list_print.scm
Normal file
|
@ -0,0 +1,40 @@
|
|||
(import (chicken format)) ; format printf
|
||||
(import srfi-13) ; string-copy!
|
||||
|
||||
(define (string-repeat str n)
|
||||
(let* ((slen (string-length str))
|
||||
(res (make-string (* slen n))))
|
||||
(do ((i 0 (+ i slen))) ((= i n) res)
|
||||
(string-copy! res i str))))
|
||||
|
||||
(define arrow "-->")
|
||||
(define spacer (string-repeat " " (string-length arrow)))
|
||||
|
||||
(define (print-cell val)
|
||||
(printf "| ~a | *-|" val))
|
||||
|
||||
;;; Convert any value to a string, a hack though
|
||||
(define (any->number val)
|
||||
(format #f "~a" val))
|
||||
|
||||
(define (print-top-bottom lst)
|
||||
(for-each
|
||||
(lambda (e)
|
||||
(let ((vals (any->number e)))
|
||||
(printf "+~a+---+~a"
|
||||
(string-repeat "-" (+ (string-length vals) 2)) spacer)))
|
||||
lst)
|
||||
(display "\n"))
|
||||
|
||||
;;; Creates a ASCII-art representation of a list
|
||||
(define (list-ascii lst)
|
||||
(print-top-bottom lst)
|
||||
(for-each (lambda (c)
|
||||
(print-cell c)
|
||||
(display arrow))
|
||||
lst)
|
||||
(display "NIL\n")
|
||||
(print-top-bottom lst))
|
||||
|
||||
(list-ascii '(1 2 3 4))
|
||||
(list-ascii '("foo" #x27 "aaaeeeiiiooouuu" 100 28/8))
|
Loading…
Add table
Reference in a new issue