I'm learning Racket (but probably answer will be similar in any Scheme and scheme-derived language) and wonder how to filter out false (#f) values from a given list. The best I came up with is:
(filter (lambda (x)
(not (eq? x #false)))
'("a" "b" #f 1 #f "c" 3 #f))
'("a" "b" 1 "c" 3) ;; output
However, I guess there has to be a simpler solution.
The list is the fundamental data structure of Racket. A list is a sequence of values. A list can contain any kind of value, including other lists. A list can contain any number of values.
First find the lenght of a list by cdring it down. Then use list-ref x which gives the x element of the list. For example list-ref yourlistsname 0 gives the first element (basically car of the list.) And (list-ref yourlistsname (- length 1)) gives the last element of the list.
You can just do
(filter identity '("a" "b" #f 1 #f "c" 3 #f))
as anything not #f is considered true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With