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

时间:2015-11-01 20:33:49

标签: scheme racket

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

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

sourceSets.main {
    jni.srcDirs = [] // This prevents the auto generation of Android.mk
    jniLibs.srcDir 'src/main/jni'
}

4 个答案:

答案 0 :(得分:1)

#!/bin/bash # Panned and zoomed animation # Mark Setchell # steps=40 # Final x offset from top left finalx=2800 # Final y offset from top left finaly=1400 # Initial & Final width initw=4000 finalw=1000 # Initial & Final height inith=3200 finalh=800 # Remove anything from previous attempts rm frame-*jpg 2> /dev/null for i in $(seq 0 $steps); do ((x=finalx*i/steps)) ((y=finaly*i/steps)) ((w=initw-(i*(initw-finalw)/steps))) ((h=inith-(i*(inith-finalh)/steps))) echo $i,$x,$y,$w,$h name=$(printf "frame-%03d.jpg" $i) convert elcap.jpg -crop ${w}x${h}+${x}+${y} -resize 200x160 "$name" done convert -delay 20 frame* anim.gif 将过程作为第一个参数,并将其应用于给定列表中的每个元素。因为你在计算一些列表是错误的。

map将过程作为第一个参数,起始值作为第二个参数和一个或多个列表。它将过程与元素和起始值(或中间值)一起应用,并且过程可以决定下一个中间值。例如。你可以用它来计算一个清单:

foldl

您可以轻松地将此更改为仅在(define (my-length lst) (foldl (lambda (x acc) (+ acc 1)) 0 lst)) (my-length '(a b c)) ; ==> 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)) 的程序进行同样的改动,只能在某些情况下计算。

答案 1 :(得分:1)

以下不使用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

答案 2 :(得分:0)

(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

答案 3 :(得分:0)

一个简单的功能开始

#lang racket

(define (comp-list threshold list-nums)
  (define (my-filter-function num)
    (<  num threshold))
  (length (filter my-filter-function list-nums)))

define

替换lambda
#lang racket

(define (comp-list threshold list-nums)
  (length (filter (lambda (num) (< num threshold))
                  list-nums)))

球拍的filter

的实施

DrRacket 中突出显示过程名称并右键单击并选择“跳转到其他文件中的定义”将允许查看源代码。 filter的源代码很有启发性:

  (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)))))