簡體   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