要求是使用drracket(scheme)build-list来生成(list "a" "aa" "aaa" "aaaa" "aaaaa")
。
我试过了foldr
,但只给了#34; aaaaa"
(define (build-a-string n)
(foldr string-append "" (build-list n "a")))
答案 0 :(得分:0)
(build-list 5 (lambda (i)
(make-string (add1 i) #\a)))
答案 1 :(得分:0)
这是一个使用“fold”的解决方案:
#lang racket
(define (build-as n)
(for/fold ([as '("")]) ([_ n])
(define last-a (first as))
(define new-a (string-append "a" last-a))
(cons new-a as)))
(build-as 5)
以下是简短版本:
(define (build-as n)
(for/fold ([as '("")]) ([_ n])
(cons (string-append "a" (first as)) as)))