Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter out false values from the list in racket

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.

like image 908
radious Avatar asked Jul 29 '15 06:07

radious


People also ask

What does list do in Racket?

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.

How do you find the last element of a list in racquet?

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.


1 Answers

You can just do

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

as anything not #f is considered true.

like image 146
vukung Avatar answered Sep 21 '22 02:09

vukung