Skip to content

Instantly share code, notes, and snippets.

@neelabhg
Last active August 29, 2015 14:16
Show Gist options
  • Save neelabhg/742a3080502a63785689 to your computer and use it in GitHub Desktop.
Save neelabhg/742a3080502a63785689 to your computer and use it in GitHub Desktop.
Read and print a list
#lang racket
; Read a list from stdin with each element on a new line
(define (read-list)
(let ((x (read)))
(if (eof-object? x)
'()
(cons x (read-list)))))
; Read n elements (or less, if exhausted) into a list from stdin where each element is on a new line
(define (read-n-list n)
(if (<= n 0)
'()
(let ((x (read)))
(if (eof-object? x)
'()
(cons x (read-n-list (- n 1)))))))
; Print a list to stdout with each element on a new line
(define (print-list lst)
(if (null? lst)
(void)
(begin (display (car lst))
(newline)
(print-list (cdr lst)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment