Reputation: 93
I'm trying to define an element according to given values from another list
(define foo ; not working but this is general idea
(λ (x ys)
(for/list ([y ys])
(if (eq? x (car y))
(let ('x (cdr y))
(boolean? x)) ; x not passed, returns #f
'())))) ; or use x to do something like (eval ('and x #t))
Desired output:
>(foo 'a '((a . true) (b . false)))
#t
to show the boolean of (cdr y)
actually passes onto x (just an example that I'm trying)
Algorithm - search through ys
for which (car y)
of each element in ys
to be equal to a
and set the boolean (cdr y)
to a
. So the above x
should have boolean #t
.
Not quite understanding why it doesn't work.
Upvotes: 1
Views: 40
Reputation: 135197
I'm doing my best to understand your problem, but it's not totally clear what your objective is.
It looks like you're trying to return a single #t
/#f
value, but if you look at the documentation for for/list
, you'll see that it will always return a list.
Let's try something like this instead
(define (foo x ys)
(let loop ([ys ys])
(cond [(empty? ys) '()]
[(eq? x (caar ys)) (cdar ys)]
[else (loop (cdr ys))])))
(foo 'a '((a . true) (b . false))) ;=> 'true
(foo 'b '((a . true) (b . false))) ;=> 'false
However, if you want to convert 'true
to #t
and 'false
to #f
, you could add a helper function like so
(define (bool x)
(eq? 'true x))
(define (foo x ys)
(let loop ([ys ys])
(cond [(empty? ys) '()]
[(eq? x (caar ys)) (bool (cdar ys))]
[else (loop (cdr ys))])))
(foo 'a '((a . true) (b . false))) ;=> #t
(foo 'b '((a . true) (b . false))) ;=> #f
Upvotes: 2