'('(LIST)'NIL'NIL)应该是(hanoi('('('list)'()'())))中的lambda表达式 [英] '('(LIST) 'NIL 'NIL) should be a lambda expression in (hanoi('('(list)'()'())))

查看:171
本文介绍了'('(LIST)'NIL'NIL)应该是(hanoi('('('list)'()'())))中的lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施河内的塔.我没有打印任何内容之间我的递归调用之间,但我不断收到错误消息

I'm trying to implement the Towers of Hanoi.I'm not printing out anything between my recursive calls yet, but I keep getting an error saying

'('((LIST)'NIL'NIL)应该是lambda表达式

'('(LIST) 'NIL 'NIL) should be a lambda expression

我已经知道发生这种情况的原因是由于括号的问题,但是我似乎找不到我的问题所在.我想在尝试调用hanoi函数时在pass-list函数中发生这种情况.我的代码:

I've read that the reason this happens is because of a problem with the parenthesis, however I cannot seem to find what my problem is. I think it's happening in the pass-list function when I am trying to call the hanoi function. My code:

(defun pass-list(list)
   (hanoi('('(list)'()'())))
)

(defun hanoi ('('(1) '(2) '(3)))

    (hanoi '('(cdr 1) '(cons(car 1) 2) '(3)))

    (hanoi '('(cons(car 3)1) '(2)'(cdr 3)))
 )

推荐答案

这段代码有很多语法问题;到处都有错误的引号,看起来您正在尝试将数字用作变量,这是行不通的.您提到的特定错误消息的来源来自

This code has many syntax problems; there are erroneous quotes all over the place, and it looks like you're trying to use numbers as variables, which will not work. The source of the particular error message that you mentioned comes from

(hanoi('('(list)'()'())))

首先,请了解'x中的报价'(a b c)是形式(quote x)(quote (a b c))的简写,并且(quote anything)是在不评估anything的情况下获取anything的语法.因此,'(1 2 3)给您列表(1 2 3),而'1给您1.尽管quote只是一个符号,并且可以出现在其他列表中,所以'('(list)'()'())(quote ((quote (list)) (quote ()) (quote ())))相同,后者的结果为列表((quote (list)) (quote ()) (quote ())).由于()也可以写为nil(或NIL),因此最后一个与('(list) 'NIL 'NIL)相同.在Common Lisp中,函数调用看起来像

First, understand that the quotes in 'x and '(a b c) are shorthand for the forms (quote x) and (quote (a b c)), and that (quote anything) is the syntax for getting anything, without anything being evaluated. So '(1 2 3) gives you the list (1 2 3), and '1 gives you 1. quote is just a symbol though, and can be present in other lists, so '('(list)'()'()) is the same as (quote ((quote (list)) (quote ()) (quote ()))) which evaluates to the list ((quote (list)) (quote ()) (quote ())). Since () can also be written nil (or NIL), this last is the same as ('(list) 'NIL 'NIL). In Common Lisp, function calls look like

(function arg1 arg2 ...)

其中每个argi是形式,并且function是符号(例如,listhanoicar)或列表,在这种情况下,它必须是lambda表达式,例如(lambda (x) (+ x x)).所以,在你的行中

where each argi is a form, and function is either a symbol (e.g., list, hanoi, car) or a list, in which case it must be a lambda expression, e.g., (lambda (x) (+ x x)). So, in your line

(hanoi('('(list)'()'())))

我们有一个函数调用. functionhanoiarg1('('(list)'()'())).但是,如何评估此arg1?好吧,这是一个列表,这意味着它是一个功能应用程序.什么是function部分?是

we have a function call. function is hanoi, and arg1 is ('('(list)'()'())). But how will this arg1 be evaluated? Well, it's a list, which means it's a function application. What's the function part? It's

'('(list)'()'())

'('(list 'NIL 'NIL))

但是正如我刚才所说,唯一可以作为function的列表是lambda表达式.显然这不是lambda表达式,因此您会看到错误.

But as I just said, the only kind of list that can be function is a lambda expression. This clearly isn't a lambda expression, so you get the error that you're seeing.

我不能确定,但​​是您的目标似乎是以下目标.标记为**的行有点问题,因为您正在调用带有某些参数的hanoi,并且当它返回时(如果返回的话;在我看来,您希望在这种情况下永远递归)不要对结果进行任何操作.它会被忽略,然后进入第三行.

I can't be sure, but it looks like you were aiming for something like the following. The line marked with ** is sort of problematic, because you're calling hanoi with some arguments, and when it returns (if it ever returns; it seems to me like you'd recurse forever in this case), you don't do anything with the result. It's ignored, and then you go onto the third line.

(defun pass-list(list)
  (hanoi (list list) '() '()))

(defun hanoi (a b c)
  (hanoi (rest a) (cons (first a) b) c)  ; **
  (hanoi (cons (first c) a) b (rest c)))

如果hanoi应该接受一个列表作为参数,并且该列表应该包含三个列表(我不确定为什么要这样做,而不是让hanoi只接受三个列表参数,但这是一个不同的问题,我想),修改起来很容易;只需接受参数abc并从中提取第一,第二和第三列表,然后在递归调用中将单个列表传递给hanoi:

If hanoi is supposed to take a single list as an argument, and that list is supposed to contain three lists (I'm not sure why you'd do it that way instead of having hanoi take just three arguments, but that's a different question, I suppose), it's easy enough to modify; just take an argument abc and extract the first, second, and third lists from it, and pass a single list to hanoi on the recursive call:

(defun hanoi (abc)
  (let ((a (first abc))
        (b (second abc))
        (c (third abc)))
    (hanoi (list (rest a) (cons (first a) b) c))
    (hanoi (list (cons (first c) a) b (rest c)))))

我实际上可能在这里使用 destructuring-bind 来简化从abc中删除abc:

I'd actually probably use destructuring-bind here to simplify getting a, b, and c out of abc:

(defun hanoi (abc)
  (destructuring-bind (a b c) abc
    (hanoi (list (rest a) (cons (first a) b) c))
    (hanoi (list (cons (first c) a) b (rest c)))))

这篇关于'('(LIST)'NIL'NIL)应该是(hanoi('('('list)'()'())))中的lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆