Functional programming Racket,在列表中查找第n个元素的写入函数

Functional programming Racket,在列表中查找第n个元素的写入函数,functional-programming,scheme,racket,Functional Programming,Scheme,Racket,为了理解函数编程,请帮助我编写一个输出列表第n个元素的函数 允许的命令: define lambda cond else empty empty? first rest cons list list? = equal? and or not + - * / < <= > >= 输出为4 与上面python中的代码相比。创建局部变量计数器的函数中的等效行为是什么。退货的“关键字”是什么 输出: (列表4-5) (列出“g”h)

为了理解函数编程,请帮助我编写一个输出列表第n个元素的函数

允许的命令:

define  lambda  cond  else  empty  empty?  first  rest  cons  list

list?  =  equal?  and  or  not  +  -  *  /  <  <=  >  >=
输出为4 与上面python中的代码相比。创建局部变量计数器的函数中的等效行为是什么。退货的“关键字”是什么

输出:

(列表4-5)
(列出“g”h)
“空的”

输出:

(列表4-5)
(列出“g”h)
“空的”


看起来你自己想出了一个答案。干得好!我建议使用更通用的
nth
过程,该过程将
计数器作为参数。这允许您获取输入列表中的任何元素

(define (nth lst counter)
  (cond ((null? lst) (error 'nth "index out of bounds"))
        ((= counter 0) (first lst))
        (else (nth (rest lst) (- counter 1)))))
现在,如果您想要一个只返回第4个元素的过程,我们将创建一个专门处理泛型
nth

(define (fourth-element lst)
  (nth lst 3))
就这样。现在,我们用您的输入测试它们

(define a `(1 2 3 (4 5) 7))
(define b `(1 2 3))
(define c `((a b)(c d)(e f)(g h)(i j)))
(define d `(a b c))

(fourth-element a) ; '(4 5)
(fourth-element b) ; nth: index out of bounds
(fourth-element c) ; '(g h)
(fourth-element d) ; nth: index out of bounds
注意,当计数器超出界限时,我选择引发一个
错误
,而不是像您的程序那样返回一个值(
“empty”
)。返回值使您无法知道是否在列表中实际找到了值,或者是否返回了默认值。在下面的示例中,请注意您的过程如何无法区分这两个输入

(define d `(a b c))
(define e `(a b c ,"empty"))

; your implementation
(fourth-element e) ; "empty" 
(fourth-element d) ; "empty"

; my implementation
(fourth-element e) ; "empty" 
(fourth-element d) ; error: nth: index out of bounds
如果您不想抛出错误,有另一种方法可以对
nth
进行编码。我们可以返回第n对元素,而不是返回第n个元素,它的头部包含有问题的元素

在下面,
nth
始终返回一个列表。如果列表为空,则未找到任何元素。否则,第n个元素是结果中的
第一个
元素

(define (nth lst counter)
  (cond ((null? lst) '())
        ((= counter 0) lst)
        (else (nth (rest lst) (- counter 1)))))

(define (fourth-element lst)
  (nth lst 3))

(define a `(1 2 3 (4 5) 7))
(define b `(1 2 3))
(define c `((a b)(c d)(e f)(g h)(i j)))
(define d `(a b c))
(define e `(a b c ,"empty"))

(fourth-element a) ; '((4 5) 7)
(fourth-element b) ; '()
(fourth-element c) ; '((g h) (i j))
(fourth-element d) ; '()
(fourth-element e) ; '("empty")
希望这能让您开始考虑域(过程输入类型)和codomain(过程输出类型)

通常,您希望设计具有自然描述的过程,如:

  • n
    获取列表和数字,并始终返回列表”(最佳)
  • n
    获取一个列表和一个数字,并返回列表中的一个元素,如果找不到该元素,则引发异常”(很好,但现在必须处理错误)
避免像这样的程序

  • nth
    获取一个列表和一个数字,并返回列表中的一个元素或字符串文本
    “empty”
    (如果找不到该元素)。”

通过考虑程序的域和密码域,您可以了解在程序的各个部分中插入函数时函数将如何工作。使用许多定义不好的域的过程会导致灾难性的意大利面代码。相反,定义良好的过程可以像构建块一样组装,只需很少(或不需要)胶水代码。

看起来你自己找到了答案。干得好!我建议使用更通用的
nth
过程,该过程将
计数器作为参数。这允许您获取输入列表中的任何元素

(define (nth lst counter)
  (cond ((null? lst) (error 'nth "index out of bounds"))
        ((= counter 0) (first lst))
        (else (nth (rest lst) (- counter 1)))))
现在,如果您想要一个只返回第4个元素的过程,我们将创建一个专门处理泛型
nth

(define (fourth-element lst)
  (nth lst 3))
就这样。现在,我们用您的输入测试它们

(define a `(1 2 3 (4 5) 7))
(define b `(1 2 3))
(define c `((a b)(c d)(e f)(g h)(i j)))
(define d `(a b c))

(fourth-element a) ; '(4 5)
(fourth-element b) ; nth: index out of bounds
(fourth-element c) ; '(g h)
(fourth-element d) ; nth: index out of bounds
注意,当计数器超出界限时,我选择引发一个
错误
,而不是像您的程序那样返回一个值(
“empty”
)。返回值使您无法知道是否在列表中实际找到了值,或者是否返回了默认值。在下面的示例中,请注意您的过程如何无法区分这两个输入

(define d `(a b c))
(define e `(a b c ,"empty"))

; your implementation
(fourth-element e) ; "empty" 
(fourth-element d) ; "empty"

; my implementation
(fourth-element e) ; "empty" 
(fourth-element d) ; error: nth: index out of bounds
如果您不想抛出错误,有另一种方法可以对
nth
进行编码。我们可以返回第n对元素,而不是返回第n个元素,它的头部包含有问题的元素

在下面,
nth
始终返回一个列表。如果列表为空,则未找到任何元素。否则,第n个元素是结果中的
第一个
元素

(define (nth lst counter)
  (cond ((null? lst) '())
        ((= counter 0) lst)
        (else (nth (rest lst) (- counter 1)))))

(define (fourth-element lst)
  (nth lst 3))

(define a `(1 2 3 (4 5) 7))
(define b `(1 2 3))
(define c `((a b)(c d)(e f)(g h)(i j)))
(define d `(a b c))
(define e `(a b c ,"empty"))

(fourth-element a) ; '((4 5) 7)
(fourth-element b) ; '()
(fourth-element c) ; '((g h) (i j))
(fourth-element d) ; '()
(fourth-element e) ; '("empty")
希望这能让您开始考虑域(过程输入类型)和codomain(过程输出类型)

通常,您希望设计具有自然描述的过程,如:

  • n
    获取列表和数字,并始终返回列表”(最佳)
  • n
    获取一个列表和一个数字,并返回列表中的一个元素,如果找不到该元素,则引发异常”(很好,但现在必须处理错误)
避免像这样的程序

  • nth
    获取一个列表和一个数字,并返回列表中的一个元素或字符串文本
    “empty”
    (如果找不到该元素)。”

通过考虑程序的域和密码域,您可以了解在程序的各个部分中插入函数时函数将如何工作。使用许多定义不好的域的过程会导致灾难性的意大利面代码。相反,定义良好的过程可以像构建块一样组装,只需很少(或不需要)胶水代码。

下面是如何用Python编写它:

def nth(lst,idx=0):
如果(len(lst)==0):
返回“空”
elif(idx==0):
返回lst[0]
其他:
返回n(lst[1:],idx-1)
n([1,2,3],1)
# ==> 2
def第四元件(lst):
返回第n次(第一次,第4次)
方案/球拍相同:

(定义(第n个lst idx)
(cond((空?lst)空);比((长度lst)0)更有效
(=idx 0)(第一个lst))
(其他(第n个(其余第1个)(-idx1)))
(第n'(1 2 3)1)
; ==> 2
(定义(第四元素lst)
(北纬一度四度)
return
没有关键字。每个表单都返回最后计算的代码:

(如果(<4 x)
(第十栏)
(开始
(显示“打印此文件”)
(foo x)))
if
返回
(条x)
的结果或打印“print This”,然后返回
(foo x)
的结果。原因是
if
的两个结果都是尾部表达式