简体   繁体   English

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

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

I'm trying to filter out a list based off of a predicate I wrote myself, but when I run the filter, I get 我试图根据我自己写的谓词过滤掉一个列表,但是当我运行过滤器时,我得到

ERROR: Wrong value to apply: #f

The code of the predicate: 谓词的代码:

;;;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))
      )
    )
)

this is my implementation of the filter (it is in a let statement): 这是我对过滤器的实现(在let语句中):

(updated-strlist(filter notwhitespace? strlist))

any ideas? 有任何想法吗? thanks! 谢谢!

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

So (call-with-current-continuation ...) in your code is wrappen in extra parentheses which means that Scheme should take the result and run it as a procedure the moment it gets it. 因此,您的代码中的(call-with-current-continuation ...)用多余的括号括起来,这意味着Scheme应该获取结果,并在获得结果时立即将其作为过程运行。

Usually in a LISP evaluator apply is the procedure that runs procedures. 通常在LISP评估器中apply是运行过程的过程。 eg. 例如。

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

You code however tries to do this (#f) and since #f is not a procedure apply cannot run it as if it were one. 但是,您的代码将尝试执行此操作(#f)并且由于#f不是apply的过程,因此无法像运行#f那样运行它。

A comment on the rest there. 对其余的评论。 If you are not using return you really shouldn't use call-with-current-continuation at all and for-each does sonething entirely different than you think. 如果您不使用return那么您根本就不应该完全使用“ call-with-current-continuationfor-each发音与您想象的完全不同。 nowhitespace? will always evaluate to #f when you've fixed your problems because the last expression in the body of the continuation lambda is #f (the returned value). 解决问题后,它将始终评估为#f ,因为延续lambda主体中的最后一个表达式为#f (返回值)。

I guess you are looking for something like: 我想您正在寻找类似的东西:

;; 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