繁体   English   中英

方案过滤器-“应用的值错误:#f”

[英]Scheme filters - “wrong value to apply: #f”

我试图根据我自己写的谓词过滤掉一个列表,但是当我运行过滤器时,我得到

ERROR: Wrong value to apply: #f

谓词的代码:

;;;Predicate for checking if a string is not empty or full of whitespaces
(define (notwhitespace? str)
  (if (equal? str "") #F (
    (call-with-current-continuation
     (lambda (return)
      (for-each
       (lambda (c)
         (if (not (char-whitespace? c)) #T #F))
        (string->list str))
        #F))
      )
    )
)

这是我对过滤器的实现(在let语句中):

(updated-strlist(filter notwhitespace? strlist))

有任何想法吗? 谢谢!

不要写(#f) ,应该是#f

因此,您的代码中的(call-with-current-continuation ...)用多余的括号括起来,这意味着Scheme应该获取结果,并在获得结果时立即将其作为过程运行。

通常在LISP评估器中apply是运行过程的过程。 例如。

(define (test) (display "hello"))
(define (get-proc) test)
((get-proc)) ; ==> undefined, displays "hello"

但是,您的代码将尝试执行此操作(#f)并且由于#f不是apply的过程,因此无法像运行#f那样运行它。

对其余的评论。 如果您不使用return那么您根本就不应该完全使用“ call-with-current-continuationfor-each发音与您想象的完全不同。 nowhitespace? 解决问题后,它将始终评估为#f ,因为延续lambda主体中的最后一个表达式为#f (返回值)。

我想您正在寻找类似的东西:

;; needs to import (srfi :1)
(define (notwhitespace? str)
  (every (lambda (x) (not (char-whitespace? x)))
         (list->string str)))

;; needs to import (srfi :13)
(define (notwhitespace2? str)
  (not (string-index str char-whitespace?)))

暂无
暂无

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

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