【问题标题】:Racket -UpperCase球拍-大写
【发布时间】:2012-11-11 10:22:30
【问题描述】:
; defining the procedure char_toupper to convert a lower case character to upper case
(define char_toupper (lambda (myChar)
                       ; defining x as the ASCII value of myChar
                       (define x (char->integer myChar))
                       ; defining y as x-32
                       (define y (- x 32))
                       ; if statement for while x is less than 91 (already uppercase)
                       (if (< x 91)
                            ; if it is already uppercase, just display it
                            (display myChar)
                            ; otherwise, if x is greater than 96 (lowercase)
                            (if (> x 96)
                                ; then display the character equivalent to the ASCII value given by y
                                (display (integer->char y))))))

(define string_toupper (lambda (myString newString i)       
                         (if (< i (string-length myString))
                             (string_toupper myString (string-append newString (char_toupper (string-ref myString i))) (+ 1 i)))

                         (display newString)))
(string_toupper (read) "" 0)

这会将每个字符转换为大写,并显示它。但我不断收到错误,我可以找到它。任何帮助。谢谢

【问题讨论】:

  • 你不断得到的错误是?
  • 字符串长度:预期违反合同:字符串?
  • read 不一定返回字符串。它返回一个值,但该值可能是一个字符串、一个符号、一个数字、一个列表,或者......取决于用户输入的内容。所以你可能想使用 read-line 代替:@ 987654321@
  • 还有char_toupper函数实际上返回void...

标签: scheme racket


【解决方案1】:

在 Racket 中,你必须写 when 而不是在单臂情况下的 if

即把下面表达式中的if改为when

(if (> x 96)
    ; then display the character equivalent to the ASCII value given by y
    (display (integer->char y)))

还要注意string-upcase 是内置的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 2011-02-22
  • 2013-11-22
相关资源
最近更新 更多