scheme - 如何在 andmap 中的 lambda 函数中返回值

标签 scheme lisp racket

我正在学习 Racket,我不知道如何在 andmap 函数中返回一个值:

(define iguales 0)

(andmap
    (lambda (x y)
        (cond 
          ((eq? (last x) (last y)) 
             (set! iguales (add1 iguales)))))
    casos extension)

如果我在 lambda (x y) 函数的末尾添加 iguales 我认为它会为每个 xy 值。

在该 lambda 函数中放置返回值的正确位置在哪里?

最佳答案

主体返回最后一个形式的值。

这个:

(cond ((eq? (last x) (last y)) 
       (set! iguales (add1 iguales)))

返回 #fset! 形式的结果。

为了在任何情况下从 lambda 返回 iguales:

(lambda (x y)
  (cond ((eq? (last x) (last y)) 
         (set! iguales (add1 iguales))))
  iguales)

然后 andmap 表单将返回 #fcasosextension 中较短者的长度(明白为什么了吗?)。

关于scheme - 如何在 andmap 中的 lambda 函数中返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495869/

相关文章:

lisp - 如何在 plt 方案中使用 'destroy/dispose' frame%?

scheme - 仅使用 "The Little Schemer"中的表单展平列表

macros - 普通口齿不清 : how can a macro define other methods/macros with programmatically generated names?

emacs - eval-buffer 在设置 CLISP/SLIME 时不做任何事情

scheme 如何返回一个表单符号 + *

方案:添加到记录列表

list - 如何在 Racket (方案)中将列表分成均匀大小的 block ?

lisp - emacs lisp 无法启动

recursion - 查找列表中的子列表

c++ - Racket/Scheme C++ 扩展和继承