尝试遍历字符串列表并将计数添加到字符串的末尾

Trying to go through a list of strings and add count to then end of strings

我正在尝试使用 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)))

我对球拍比较陌生,在编写这个迭代函数时遇到了问题

以下是我在 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-append 加上 number->string 更具可读性。)