Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scheme-检索数字中出现的数字_Scheme - Fatal编程技术网

Scheme-检索数字中出现的数字

Scheme-检索数字中出现的数字,scheme,Scheme,我正在进行bert方案练习,这项练习让我很难接受: 示例:(n-发生544555 5)=>4 你知道怎么数数吗 我的想法是: (define (occurences d n) (if (equal? (remainder d 10) n) (add1 (occurences (quotient d 10) n)) (occurences (quotient d 10) n) )) 因此,在例如1223 2的示例中,它将: 检查3是否等于2 说不

我正在进行bert方案练习,这项练习让我很难接受:

示例:(n-发生544555 5)=>4

你知道怎么数数吗

我的想法是:

 (define (occurences d n)
   (if (equal? (remainder d 10) n)
       (add1 (occurences (quotient d 10) n))
       (occurences (quotient d 10) n)
       ))
因此,在例如1223 2的示例中,它将:

  • 检查3是否等于2
  • 说不,然后继续,再打122 2
  • 检查2是否为2
  • 说“是”,然后在通话中添加1和12 2(计数为1)
  • 检查2是否为2
  • 说“是”,然后在通话中添加1和12(计数为2)
  • 检查1是否等于2
  • 说不就做不到

  • 您缺少递归的基本情况;如果d为0,则需要停止。你把n和d混淆了好几次:

    (define (occurences d n)
      (if (= 0 d)
          (if (= 0 n) 1 0)  ; base case
          (if (= (remainder d 10) n)
              (add1 (occurences (quotient d 10) n))
              (occurences (quotient d 10) n))))
    
    测试:

    > (occurences 544555 5) 
    4
    
    参数(d和n)演变如下:

    d=544555  n=5
    d=54455  n=5
    d=5445  n=5
    d=544  n=5
    d=54  n=5
    d=5  n=5
    d=0  n=5
    

    在边缘情况下,此操作将失败:
    (出现次数0)
    应返回
    1
    ,但返回的是
    0