【问题标题】:How to filter out false values from the list in racket如何从球拍列表中过滤掉错误值
【发布时间】:2015-10-19 23:17:07
【问题描述】:

我正在学习 Racket(但在任何 Scheme 和 Scheme 派生语言中的答案可能都相似),并且想知道如何从给定列表中过滤掉错误 (#f) 值。我想出的最好的是:

(filter (lambda (x)
           (not (eq? x #false)))
        '("a" "b" #f 1 #f "c" 3 #f))

'("a" "b" 1 "c" 3) ;; output

但是,我想必须有一个更简单的解决方案。

【问题讨论】:

    标签: scheme lisp racket


    【解决方案1】:

    你可以这样做

    (filter identity '("a" "b" #f 1 #f "c" 3 #f))
    

    因为任何不是#f 的东西都被认为是真实的。

    【讨论】:

    • 另外,您可能会看到使用values 而不是identity 的球拍代码(例如,当有人使用#lang racket/base 并且不想(require racket/function) 只是为了获得identity )。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多