[英]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-continuation
, for-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.