【问题标题】:Check if a list has uppercase or lowercase letters or numbers in a list in racket检查列表是否在球拍列表中包含大写或小写字母或数字
【发布时间】:2020-06-09 16:41:11
【问题描述】:

如何创建一个检查函数 如果一个列表在球拍中至少有一个大写或小写字母,(并且可能在列表中包含数字)

并且不包含特殊字符(没有空格,没有特殊字符)

(alphabet (list "abc" "ABC" "aBC" "AbC")) ⇒ true
(list "9wa123re1" "0w1e2a3r4")) ⇒ true

【问题讨论】:

    标签: functional-programming scheme racket


    【解决方案1】:
    (define check-lower-char
      (lambda (input)
        (andmap (lambda(s)
                  (andmap (lambda (c)
                            (or (not (char-alphabetic? c))
                                (char-lower-case? c)))
                          s))
                (map string->list input))))
    
    
    (check-lower-char (list "abc" "ABC" "aBC" "AbC")))  ;; => #f
    (check-lower-char (list "9wa123re1" "0w1e2a3r4"))   ;; => #t
    

    【讨论】:

      【解决方案2】:
      #lang racket 
      
      (define (alpha+-num? clst (acc #f) (alpha 0))
        (cond ((null clst) (and (not (zero? alpha)) acc))
              ((char-alphabetic? (car clst)) (alpha+-num? (cdr clst) #t (+ alpha 1)))
              ((char-numeric? (car clst)) (alpha+-num? (cdr clst) acc alpha))
              (else #f)))
      
      (define (alpha+-num-string? s)
        (alpha+-num? (remove-duplicates (string->list s))))
      
      (define (alpha+-num-string-list? sl (acc #f))
        (cond ((null sl) acc)
              ((alpha+-num-string? (car sl)) (alpha+-num-string-list? (cdr sl)))
              (else #f)))
      
      

      你想要的是alpha+-num-string-list?

      【讨论】:

        【解决方案3】:
        #lang racket
        
        ;; for one string is the test:
        (define (alpha-and-perhaps-numeric? s)
          (regexp-match #rx"^([0-9]*[a-zA-Z]+[0-9]*)+$" s))
        
        ;; for a list of strings is the test:
        (define (alpha-list? l)
          (for/and ((x l))
            (alpha-and-perhaps-numeric? x)))
        
        • [0-9]* 表示:零个或多个 (*) 数字 ([0-9])
        • [a-zA-Z]+ 表示:至少一个或多个 (+) 小写或大写字母 ([a-zA-Z])
        • [0-9]* 后面可能跟零个或多个数字。
        • ( )+ 整个构造至少匹配一次,如果不是更多次。
        • ^ $ 确保这个东西从字符串的开头到结尾匹配,没有间隙。 因此,不能使用此模式构建的任何其他字符串都将返回 #f。 (任何包含字符串的非字母数字字符)。

        【讨论】:

        • 对不起这是什么语言。我不熟悉这种语言。
        • @loren 普通球拍。 #rx" " 表示正则表达式字符串。这是内置在球拍中的。它简称为“正则表达式”。许多编程语言都将其内置为字符串的领域特定语言。
        • 谢谢你,但我还没有学习内置函数,只是基本函数。
        • 对不起,这仍然使用抽象函数。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        • 2021-06-17
        • 2017-03-13
        相关资源
        最近更新 更多