i finally figured out how to close files in racket

main
m455 2018-03-19 16:56:52 -04:00
parent 2bf06764ff
commit 35f28df5d3
1 changed files with 9 additions and 8 deletions

View File

@ -1,7 +1,7 @@
#! /usr/bin/env racket #! /usr/bin/env racket
#lang racket/base #lang racket/base
(define (display-hash hash-list key) (define (displayln-hash-ref hash-list key)
(displayln (hash-ref hash-list key))) (displayln (hash-ref hash-list key)))
(define messages (hash (define messages (hash
@ -21,26 +21,27 @@
(define (open/create-file path) (define (open/create-file path)
(let ([path (expand-user-path path)]) (let ([path (expand-user-path path)])
(open-output-file path (let ([opened-file (open-output-file path
#:mode 'text #:mode 'text
#:exists 'can-update))) #:exists 'can-update)])
(close-output-port opened-file))))
(define (prompt-user prompt-message) (define (prompt-user prompt-message)
(display-hash messages prompt-message) (displayln-hash-ref messages prompt-message)
(let ([user-input (read-line)]) (let ([user-input (read-line)])
(cond (cond
[(member user-input (hash-ref y/n 'yes)) [(member user-input (hash-ref y/n 'yes))
(display-hash messages 'creating-file) (displayln-hash-ref messages 'creating-file)
(open/create-file "~/.rodo")] (open/create-file "~/.rodo")]
[(member user-input (hash-ref y/n 'no)) [(member user-input (hash-ref y/n 'no))
(display-hash messages 'terminating)] (displayln-hash-ref messages 'terminating)]
[else [else
(display-hash messages 'choose-y/n) (displayln-hash-ref messages 'choose-y/n)
(prompt-user 'file-not-found)]))) (prompt-user 'file-not-found)])))
(define (todo-list-exist?) (define (todo-list-exist?)
(if (file-exists? (expand-user-path "~/.rodo")) (if (file-exists? (expand-user-path "~/.rodo"))
(display-hash messages 'file-exists) (displayln-hash-ref messages 'file-exists)
(prompt-user 'file-not-found))) (prompt-user 'file-not-found)))
(todo-list-exist?) (todo-list-exist?)