返回大写字母

时间:2019-03-25 05:58:04

标签: racket

我正在编写一个可以从输入字符串返回大写字母的函数。当我显示它时效果很好。但是,有人能告诉我如何返回输出字符串,而不只是显示它吗?

update people.t1, people.t2
set 
    id_avito = people.t2.id, 
    lnk_avito = people.t2.link, 
    all_price = people.t2.price,
    all_date = people.t2.date, 
    all_adr = people.t2.adr,
    all_usl_name = people.t2.usl_name 
where id_avito != people.t2.id
and all_tel= people.t2.tel 
and all_usl_type = people.t2.usl_type

2 个答案:

答案 0 :(得分:2)

如果您要从函数返回数据,就像在这里返回字符串一样,建议您从基本的for循环看一下它的变体,例如for/list,{{3 }},for/vectorfor/hash。在这种情况下,for/fold可以提供帮助:

(define (convert input)
  (list->string
   (for/list ([i input] #:when (char-alphabetic? i))
     (char-upcase i))))

使用它:

> (convert "ab1c23")
"ABC"

答案 1 :(得分:1)

这是一种可能的解决方案:

(define (convert input)
  (list->string
   (foldr (lambda (chr acc)
            (if (char-alphabetic? chr)
                (cons (char-upcase chr) acc)
                acc))
          '()
          (string->list input))))

我们需要将结果累加到某个地方,而不是逐个打印char。为此,我们使用foldr处理一个字符列表,将字母字符大写,而忽略其他字符。这将生成一个字符列表,我们使用list->string将其转换回字符串。它按预期工作:

(convert "ab1c23")
=> "ABC"