Scheme 将数字列表与变量进行比较

Scheme 将数字列表与变量进行比较,scheme,racket,Scheme,Racket,下面的函数用于将列表中的每个数字(第二个参数)与第一个参数进行比较,并对列表中大于第二个参数的每个num进行计数,然后返回列表中大于“阈值”的元素总数 我的代码没有运行,因为我试图学习Dr.Racket中的递归是如何工作的,但我似乎无法理解。我只是感到沮丧,所以只知道下面的代码不应该接近工作;我不喜欢函数式编程,哈哈 (define (comp-list threshold list-nums) (cond [(empty? list-nums) 0] [(cons? li

下面的函数用于将列表中的每个数字(第二个参数)与第一个参数进行比较,并对列表中大于第二个参数的每个num进行计数,然后返回列表中大于“阈值”的元素总数

我的代码没有运行,因为我试图学习Dr.Racket中的递归是如何工作的,但我似乎无法理解。我只是感到沮丧,所以只知道下面的代码不应该接近工作;我不喜欢函数式编程,哈哈

(define (comp-list threshold list-nums)
  (cond [(empty? list-nums) 0]
        [(cons?  list-nums) (let {[my-var 0]}
                              (map (if (> threshold (first list-nums)) 
                                       threshold 2) list-nums ))]))

map
将过程作为第一个参数,并将其应用于给定列表中的每个元素。因为你在数一些东西,所以列一个清单是错误的

foldl
将过程作为第一个参数,将起始值作为第二个参数,并将一个或多个列表。它使用元素和起始值(或中间值)应用程序,程序get决定下一个中间值。你可以用它来计算一个列表:

(定义(我的长度lst)
(foldl(λ(x acc)(+acc 1))
0
(lst)
(我的长度’(abc));==>3.
x
大于某个阈值时,您可以轻松地将其更改为仅计数,只需计算为
acc
,在不增加值时保持不变

更新

my length
的递归解决方案:

(define (my-length lst)
  ;; auxiliary procedure since we need
  ;; an extra argument for counting
  (define (aux lst count)
    (if (null? lst)
        count
        (aux (cdr lst)
             (+ count 1))))
  ;; call auxiliary procedure
  (aux lst 0))
必须对程序进行相同的修改,将其更改为
foldl
,以便仅在某些情况下计数

(定义(成分列表阈值列表NUM)
(define (comp-list threshold list-nums)
  (cond 
    [(empty? list-nums)  ; there are 0 elements over the threshold in an empty list
     0]
    [(cons?  list-nums)  ; in a constructed list, we look at the the first number
     (cond
       [(< threshold (first list-nums)) 
        (+ 1                                        ; the first number is over
           (comp-list threshold (rest list-nums))]  ; add the rest 
       [else 
        (comp-list threshold (rest list-nums))])])) ; the first number is lower
(续) [(空?列表nums);空列表中有0个元素超过阈值 0] [(cons?list nums);在构造的列表中,我们查看第一个数字 (续) [(<阈值(第一个列表nums)) (+1;第一个数字结束 (comp list threshold(rest list nums))];添加其余的 [其他 (comp list threshold(rest list nums))]);第一个数字较低
以下内容不使用foldl的lambda(并且是递归的)-您能理解它是如何工作的吗

(define (comp-list threshold list-nums)
  (cond [(empty? list-nums) 0]
        [else
         (cond [(> (car list-nums) threshold) (+ 1 (comp-list threshold (cdr list-nums)))]
               [else (comp-list threshold (cdr list-nums))])]))
测试:

> (comp-list 1 '(1 1 2 2 3 3))
4
> (comp-list 2 '(1 1 2 2 3 3))
2
> (comp-list 3 '(1 1 2 2 3 3))
0
一个简单的功能启动 球拍的
过滤器的实现
DrRacket中,突出显示过程名称,右键单击并选择“跳转到其他文件中的定义”将允许查看源代码。
过滤器的源代码具有指导意义:

  (define (filter f list)
    (unless (and (procedure? f)
                 (procedure-arity-includes? f 1))
      (raise-argument-error 'filter "(any/c . -> . any/c)" f))
    (unless (list? list)
      (raise-argument-error 'filter "list?" list))
    ;; accumulating the result and reversing it is currently slightly
    ;; faster than a plain loop
    (let loop ([l list] [result null])
      (if (null? l)
        (reverse result)
        (loop (cdr l) (if (f (car l)) (cons (car l) result) result)))))

你能详细说明一下“lambda”在做什么以及“acc”是什么意思吗?
lambda
只是制作程序的秘密词汇。
((lambda(x)(+x10))5);=>15
acc
只是一个普通的参数名,用来表示它是一个累加值。你可以选择你想调用它们的任何名称,但是使用一些普通的名称来表示它们的用途是一种很好的方法,可以使代码更易于阅读。例如,
lst
是一个普通的参数名,它支持获取我还没有学会fold1,也没有学会lambda关键字。我是否可以使用递归检查每个列表项是否小于某个值?@CompScientist在上面的lambda函数中,新结果总是
(+acc 1)
,无论
x
是什么。但是您可以选择返回
acc
(+acc 1)
取决于
x
是否小于或大于阈值。@CompScientist添加了一个递归版本。您仍然需要修复它才能使用阈值。好吧!我将研究这个问题,看看您是如何得出这个结论的,谢谢。我在HtDP中使用了设计配方:为什么在第一部分的
else
中使用新的
cond
cond
?带有3个测试表达式的
cond
更具可读性。我这样做只是为了分离出结束条件。
#lang racket

(define (comp-list threshold list-nums)
  (length (filter (lambda (num) (< num threshold))
                  list-nums)))
  (define (filter f list)
    (unless (and (procedure? f)
                 (procedure-arity-includes? f 1))
      (raise-argument-error 'filter "(any/c . -> . any/c)" f))
    (unless (list? list)
      (raise-argument-error 'filter "list?" list))
    ;; accumulating the result and reversing it is currently slightly
    ;; faster than a plain loop
    (let loop ([l list] [result null])
      (if (null? l)
        (reverse result)
        (loop (cdr l) (if (f (car l)) (cons (car l) result) result)))))