我目前正在研究Scheme,根据我的理解,过程可以接受任意数量的参数。
我一直在尝试着玩弄这个功能,但是我还无法完全理解它。
例如,假设我想根据用户提供的信息编写欢迎消息。
如果用户提供了名字和姓氏,则程序应该输出:
Welcome, <FIRST> <LAST>!
;; <FIRST> = "Julius", <LAST>= "Caesar"
Welcome, Julius Caesar!
否则,程序应该参考一个默认值,该默认值指定为:
Welcome, Anonymous Person!
我有以下代码大纲,但不知道如何最终完成。
(define (welcome . args)
(let (('first <user_first>/"Anonymous")
('last <user_last>/"Person"))
(display (string-append "Welcome, " first " " last "!"))))
使用示例:
(welcome) ;;no arguments
--> Welcome, Anonymous Person!
(welcome 'first "John") ;;one argument
--> Welcome, John Person!
(welcome 'first "John" 'last "Doe") ;;two arguments
--> Welcome, John Doe!
任何帮助都非常感激!
(define (welcome #:first 名字 #:last 姓氏 . 其他参数) ...)
。 - Alex Knauth