Last active
August 29, 2015 14:16
-
-
Save neelabhg/742a3080502a63785689 to your computer and use it in GitHub Desktop.
Read and print a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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