尝试浏览字符串列表并将计数添加到字符串结尾

时间:2016-03-11 02:23:57

标签: string scheme racket

我正在尝试使用racket simple-qr库并接收一个字符串列表,这些字符串将成为网站并为每个项目创建qrs。

这就是我所拥有的:

#lang racket
(require simple-qr)

;;auto makes a qr for the main source of simple-qr
(qr-code "https://github.com/simmone" "gitSource.png")

;;asking the user to imput a string so that
;;they can create their own qr code
;;they can also name it themselves
(define (makeQRForME mystring namestring)
  (qr-code mystring (string-append namestring ".png")))

(define count 0)

(define (addqrlist lst)
  (if (null? lst) lst
    (makeQRForME (car lst) (string-append "stringQR"(number->string(+ 1 count)))))
    (addqrlist (rest lst)))

我对球拍很陌生,并且在编写这个迭代函数时遇到了麻烦

1 个答案:

答案 0 :(得分:2)

以下是我在Racket中编写函数的方法:

(define (make-qr-codes texts)
  (for ((text (in-list texts))
        (count (in-naturals)))
    (qr-code text (format "stringQR~a.png" count))))

但是,如果您正在尝试查看如何修复您的版本,请按以下步骤操作:

(define (addqrlist lst)
  (let loop ((rest lst)
             (count 0))
    (unless (null? rest)
      (makeQRForMe (car rest) (format "stringQR~a" count))
      (loop (cdr rest) (add1 count)))))

(您的版本混合使用carrest,而不是carcdrfirstrest。我改变了它始终使用carcdr。此外,使用format代替string-appendnumber->string更易读。)