Reputation: 1
Given a list define:
(define list
'((a1 20 (1 a2) (3 a3))
(a2 10 (2 a4))
(a3 10 (1 a5) (2 a6) (3 a7))
(a4 21)
(a5 12)
(a6 6 (1 a4) (2 a5))
) )
How do you write a function (sub 'a1) would return a list (a2 a3). I can find the list ((1 a2) (3 a3)) with the following code:
(define (sub val)
(cdr(cdr(cond ((assq val list))
(else 0)))))
But what do I do after that to get only the elements I want? I tried doing some recursion but that just lead in circles. Any advice would be appreciated.
Second question: a function to get a list of only "a" with a number value less than or equal to given input i.e (test 10) would return (a2 a3 a6) I can check one a list but I'm stuck at how to go through all the list of list and append the values.
Upvotes: 0
Views: 423
Reputation: 720
All you need is the map
function. So, either use your own sub
function like this:
(map cadr (sub 'a1))
Or change your function to include the map
:
(define (sub val)
(map cadr (cdr(cdr(cond ((assq val list))
(else 0))))))
(cadr lst)
is just a shortcut for (car (cdr lst))
. My Racket
skills are a bit rusty, so I will refrain from saying that your sub
function is entirely correct or has any glitches.
Upvotes: 0