简体   繁体   English

使用递归为球拍获得第N个位置

[英]Using recursion to getNth position for Racket

I have to write a recursive call in Racket that allows us to get the Nth position of a given list. 我必须在Racket中编写一个递归调用,使我们能够获得给定列表的第N个位置。 for example (getNth 3 '(1 2 3 4)) should return back 4 and if the list is empty or if the position is non existent then return and empty list. 例如(getNth 3'(1 2 3 4))应该返回4,如果列表为空或位置不存在,则返回为空列表。

this is my code: 这是我的代码:

(define getNth
   (lambda (ls)
     (if (= ls null?) 1
         (getNth ls(append '(car ls)(cdr ls)))))) 

getting this error: getnth: arity mismatch; 得到这个错误:getnth:arity不匹配; the expected number of arguments does not match the given number expected: 1 given: 2 arguments...: 4 () 预期的参数数量与预期的给定数量不匹配:1个给定的:2个参数...:4()

if you need the recursive statement here it is: 如果您需要递归语句,则为:

base case-getNth N LS) = (), if LS = null 如果LS = null,则返回基数-getNth N LS)=()

recursive call -(getNth N LS) = (getNth N (append '(car LS)(cdr LS))), if N >= 1 如果N> = 1,则递归调用-(getNth N LS)=(getNth N(追加'(car LS)(cdr LS)))

im getting stumped I dont know how to implement this one. 即时通讯陷入困境,我不知道如何实现这一目标。

Assuming your getNth can revieve a "N" from a list (rather than a fixed N ie get3rd), your are indeed missing an argument: 假设您的getNth可以从列表中取消“ N”(而不是固定的N即get3rd),则您确实缺少一个参数:

(define getNth                     
   (lambda (n list)                   
      (cond ((null? list) '())             
            ((= n 0) (car list))              
            (else (getNth (- n 1) (cdr list))))))

Which basically is until I get to the number I want, decrement the it and "pop" the first element out until the first element of the list is the one I want ie the rest of the list. 这基本上是直到我得到想要的数字,然后递减它并“弹出”第一个元素,直到列表的第一个元素是我想要的那个元素,即列表的其余部分。

Your lambda only takes one argument the way you defined it which is exactly the error message you're having ;) 您的lambda仅采用您定义它的方式的一个参数,而这正是您收到的错误消息;)

I think I appropriately balanced my parenthesis :) 我想我适当地平衡了括号:)

A trick with recursion: it's generic concept is easy: 递归技巧:它的通用概念很简单:

A recusrion is "if a terminal condition occurs, I'm done, return appropriate value. Else, increment the argument toward termination condition and call myself with these new arguments" 重新提醒是“如果发生了终止条件,我就完成了,返回适当的值。否则,将参数增加到终止条件并用​​这些新参数调用自己”

It gets complicated when the termination condition can have exceptions. 当终止条件可以有异常时,它将变得很复杂。 Because if your can't trivially expressed it as "if this done else redo with increment" you could make mistakes... 因为如果您不能简单地将其表示为“如果执行此操作,否则将以增量重做”,则可能会出错...

There are several issues with you code.. 您的代码有几个问题。

  1. You say you need a procedure that takes two arguments, but yours take only ls . 您说您需要一个接受两个参数的过程,但您的过程仅接受ls
  2. When ls is the empty list the result is 1 ls为空列表时,结果为1
  3. if list is not empty you recurse with two arguments, both lists. 如果list不为空,则递归使用两个参数,即两个列表。 The first is the same as the original argument which would make a infinite recursion. 第一个与进行无限递归的原始参数相同。 The second is an append between '(car lst) which evaluates to the list (car lst) and (cdr ls) (the the list ls except the first pair) 第二个是'(car lst)之间'(car lst) ,该值的值是列表(car lst)(cdr ls) (列表ls除第一对外)
(define get-nth
  (lambda (index lst)
    (if (= index 0)            ; when index is zero
        (car lst)              ; return the first element
        (get-nth (- index 1)   ; else recurse with the decrement of index
                 (cdr lst))))) ; and all but the first element (the rest) of lst

;; test
(get-nth 0 '(a))   ; ==> a
(get-nth 1 '(a b)) ; ==> b

;; However, this won't work:
(get-nth 0 '())       ; will fail
(get-nth 10 '(a b c)) ; will fail

I kind of like the fact that it doesn't work when given wrong arguments. 我有点喜欢这样的事实,即如果输入错误的参数,它将无法正常工作。 list-ref and car fails just as much if gived wrong parameters, but that is easy to fix: 如果给了错误的参数, list-refcar也会失败,但这很容易解决:

(define get-nth
  (lambda (index lst)
    (cond ((not (pair? lst)) '())        ; it should always be a pair
          ((= index 0) (car lst))        ; when index is zero, return the first element
          (else (get-nth (- index 1)     ; else recurse with the decrement of index
                         (cdr lst))))))  ; and all but the first element (the rest) of lst

Instead of just th empty list you can have a default value or perhaps throw an error with (raise 'get-nth-error) (R6RS+) 您不仅可以使用默认值,还可以使用(raise'get (raise 'get-nth-error) (R6RS +)而不是默认值来抛出空列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM