From 877a1dbf7c9cd2901a7f5de175afca1b45df2956 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 3 Nov 2022 20:55:01 +0100 Subject: prn: return value of last argument Hat tip to Tom Lord, who called this `peek`. --- mew.scm | 18 +++++++++++------- mew.svnwiki | 2 +- tests/test.mew | 3 ++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/mew.scm b/mew.scm index 78c6558..0383116 100644 --- a/mew.scm +++ b/mew.scm @@ -126,13 +126,17 @@ (for-each display args)))) (define (prn . args) - (if (null? args) - (newline) - (begin - (write (car args)) - (unless (null? (cdr args)) - (display " ")) - (apply prn (cdr args))))) + (let loop ((args args) + (ret (void))) + (if (null? args) + (begin + (newline) + ret) + (begin + (write (car args)) + (unless (null? (cdr args)) + (display " ")) + (loop (cdr args) (car args)))))) (define-syntax def (syntax-rules () diff --git a/mew.svnwiki b/mew.svnwiki index a4916ef..142fb2a 100644 --- a/mew.svnwiki +++ b/mew.svnwiki @@ -223,7 +223,7 @@ Returns true if {{}} is an unspecified value, else false. (prn . ) {{write}} all {{args}} separated by spaces and terminated by a newline -to the current output stream. +to the current output stream. Returns the value of the last argument. (puts . ) diff --git a/tests/test.mew b/tests/test.mew index 82c793a..82a2e71 100644 --- a/tests/test.mew +++ b/tests/test.mew @@ -93,7 +93,8 @@ (test-group "prn" (test "1 2 3\n" (with-output-to-string (fun () (prn 1 2 3)))) (test "\"a\" b #t\n" (with-output-to-string (fun () (prn "a" 'b #t)))) - (test "\n" (with-output-to-string (fun () (prn))))) + (test "\n" (with-output-to-string (fun () (prn)))) + (test 3 (esc ret (with-output-to-string (fun () (ret (prn 1 2 3))))))) (test-group "def" (test "define variable" -- cgit 1.4.1