【问题标题】:Scheme write a function that returns number of odd numbers in a listScheme编写一个返回列表中奇数个数的函数
【发布时间】:2013-06-26 11:14:47
【问题描述】:

我无法在 Scheme 中编写一个函数,该函数在不使用任何赋值语句的情况下返回列表中的奇数个数。我正在尝试使用谓词奇数?也是。任何帮助/提示将不胜感激。

例如:(odds '(1 2 3 4 5) // 返回 3

此外,列表是整数

【问题讨论】:

    标签: scheme racket


    【解决方案1】:

    好吧,如果不能使用赋值语句,您仍然可以使用内置过程。特别是,count 在 Racket 中可以很好地工作:

    (define (odds lst)
      (count odd? lst))
    

    ...但我猜你应该从头开始实施解决方案。自行寻找解决方案的一些提示,请填空:

    (define (odds lst)
      (cond (<???>                 ; if the list is empty
             <???>)                ; then how many odd numbers are in it?
            ((odd? <???>)          ; if the first element is odd
             (<???> (odds <???>))) ; then add one and advance recursion
            (else                  ; otherwise
             (odds <???>))))       ; just advance the recursion
    

    无论如何,它按预期工作:

    (odds '(1 2 3 4 5))
    => 3
    

    【讨论】:

      【解决方案2】:

      无论您使用 (R6RS?) Scheme 还是 Racket,这两者都适用:

      (define (odds lst)
        (length (filter odd? lst)))
      
      (define l '(1 2 3 4 5 6 7 8 9 10))
      (odds l)
      

      【讨论】:

        【解决方案3】:

        我能做到的最低水平:

        (define odds
          (lambda (lst) 
            (cond ((empty? lst) 0)
                  ((not (= 0 (modulo (car lst) 2))) (+ 1 (odds (rest lst))))
                  (else (odds (cdr lst))))))
        

        【讨论】:

          【解决方案4】:

          这是另一条线

          (define (odds L)
           (reduce + 0 (map (lambda (x) (if (odd? x) 1 0)) L)))
          

          【讨论】:

            【解决方案5】:

            这是一个函数,它返回一个基于谓词计算任何东西的函数:

            (define (counter-for predicate)
              (define (counting list)
                (if (null? list)
                    0
                    (+ (if (predicate (car list)) 1 0)
                       (counting (cdr list)))))
               counting))
            

            使用如下:

            (define odds (counter-for odd?))
            

            [更多选项]这是一个不错的递归解决方案

            (define (odds list)
              (if (null? list)
                  0
                  (+ (if (odd? (car list)) 1 0)
                     (odds (cdr list)))))
            

            这是一个尾递归解决方案:

            (define (odds list)
              (let odding ((list list) (count 0)))
                (if (null? list)
                    count
                    (odding (cdr list)
                            (+ count (if (odd? (car list)) 1 0))))))
            

            这是一个基于谓词计算任何事物的例程:

            (define (count-if predicate list)
              (if (null? list)
                  0
                  (+ (if (predicate (car list)) 1 0)
                     (count-if predicate (cdr list)))))
            

            【讨论】:

              猜你喜欢
              • 2012-09-11
              • 2021-11-21
              • 2012-09-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多