【问题标题】:Defining a Racket Function?定义球拍功能?
【发布时间】:2013-10-16 22:03:33
【问题描述】:

我应该定义函数n! (N-Factorial)。问题是我不知道该怎么做。 这是我到目前为止所拥有的,有人可以帮忙吗?我不明白 Racket 中的条件,所以解释一下就好了!

(define fact (lambda (n) (if (> n 0)) (* n < n)))

【问题讨论】:

  • 您不应该同时兼顾条件和递归。试图同时学习两者是疯狂的。请参阅单独讨论这些的教程。 ccs.neu.edu/home/matthias/HtDP2e/… 可能会有所帮助。

标签: function lambda scheme racket factorial


【解决方案1】:

您必须先好好看看documentation,这是一个非常简单的示例,但在尝试解决方案之前您必须了解基础知识,并确保您知道如何编写递归过程。一些cmets:

(define fact 
  (lambda (n)
    (if (> n 0)
        ; a conditional must have two parts:
        ; where is the consequent? here goes the advance of the recursion
        ; where is the alternative? here goes the base case of the recursion
        ) 
    (* n < n))) ; this line is outside the conditional, and it's just wrong

请注意,最后一个表达式不正确,我不知道它应该做什么,但它会引发错误。删除它,然后专注于编写条件的主体。

【讨论】:

    【解决方案2】:

    scheme(或 lisp)的诀窍是在将每组括号构建成更复杂的形式时理解每组括号之间的每一点。

    让我们从条件句开始。 if 接受 3 个参数。它计算第一个参数,如果为真,则返回第二个,如果第一个参数为假,则返回第三个。

    (if #t "some value" "some other value") ; => "some value"
    (if #f "some value" "some other value") ; => "some other value"
    (if (<= 1 0) "done" "go again") ; => "go again"
    

    cond 也可以——你可以在这里阅读条件句的球拍介绍:http://docs.racket-lang.org/guide/syntax-overview.html#%28part._.Conditionals_with_if__and__or__and_cond%29

    您可以用两种不同的方式定义函数。您正在使用匿名函数方法,这很好,但在这种情况下您不需要 lambda,因此更简单的语法是:

    (define (function-name arguments) result)
    

    例如:

    (define (previous-number n)
      (- n 1))
    (previous-number 3) ; => 2
    

    使用像你一样的 lambda 实现同样的事情,使用不同的语法(现在不要担心任何其他差异):

    (define previous-number* (lambda (n) (- n 1)))
    (previous-number* 3) ; => 2
    

    顺便说一句 - '*' 只是该名称中的另一个字符,没什么特别的(请参阅http://docs.racket-lang.org/guide/syntax-overview.html#%28part._.Identifiers%29)。一种 '!'在函数名的末尾通常意味着该函数有副作用,但是 n!在这种情况下,这是您的函数的好名称。

    让我们回到你原来的问题,把函数定义和条件放在一起。我们将使用 wiki 定义中的“递归关系”,因为它提供了一个很好的递归函数:如果 n 小于 1,则阶乘为 1。否则,阶乘是 n 乘以小于 n 的阶乘。在代码中,它看起来像:

    (define (n! n)
      (if (<= n 1) ; If n is less than 1,
        1 ; then the factorial is 1
        (* n (n! (- n 1))))) ; Otherwise, the factorial is n times the factorial of one less than n.
    

    最后一个子句比我想要的要密集一些,所以让我们在 n = 2 的情况下继续工作: (定义 n 2) (* n (n! (- n 1))) ; => (* 2 (n! (- 2 1))) (* 2 (n!1)) (* 2 1) 2

    另外,如果您使用的是 Racket,很容易确认它是否按我们预期的那样工作:

    (check-expect (n! 0) 1)
    (check-expect (n! 1) 1)
    (check-expect (n! 20) 2432902008176640000)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2014-03-16
      相关资源
      最近更新 更多