Function manageFirstList should recursively copy one element at the time to global list x.
(define test1DataA '(("a" "a") ("b" "b") ("c" "c") ("d" "d") ("e" "ok")))
(define test1DataB '(("a" "aa") ("b" "bb") ("c" "cc") ("d" "dd") ("ok" "Ir OK!")))
(define x '())
(define manageFirstList
(lambda (a b)
((cond ((not (null? b))
((append x (car a))
(manageFirstList (cdr a) b)))))))
(define ff (lambda (a b) (manageFirstList a b)))
(ff test1DataA test1DataB)
But it causes an error:
car: expects argument of type <pair>; given '()
Questions are:
- How do I update global value `x’?
- How do I concatenate lists the right way?
-
Can I call more than 1 function (or is there a workaround) like this:
((append x (car a)) (manageFirstList (cdr a) b))
EDIT: What I’m trying to do is to create a list with first value of each test1DataA list item and second value of each test1DataB list item, where test1DataA list element second value is the same as test1DataB first value of its list element.
About your questions:
(set! x <value>), you really shouldn’t, that’s not the preferred way to program using Scheme; instead you should return a new list from your procedure and if necessary, assign the returned value toxafterwardsappendprocedure concatenates two lists, that’s the right way to do it, if you ask me 🙂 . You simply call it like this:(append a b), where bothaandbmust be listsappendbetween an extra pair of parenthesis, Scheme will try to apply the value returned by(append ...)as if it were a procedure with arguments(manageFirstList (cdr a) b), and obviously it’s going to fail, sinceappendreturns a list, not a procedure. And maybe you should explain exactly what do you mean with “can I call more than 1 function” – of course you can, but what do you intend to do? combine the results from both calls? process the results independently?Anyway, the above procedure is going to fail for several reasons – you’re not considering the cases when the lists are null, you’re only recurring on one of the lists, etc.
Finally, your should make clear what is the expected result from the procedure, after calling it with
(ff test1DataA test1DataB), what is the value you want to see inx? write it as part of your question, since the text “recursively copy one element at the time to global list x” is not clear enough.Edit:
Ok, here’s my shot at answering your question, I hope I understood you correctly. Notice that I’m using the
assocprocedure for searching each second-value in the first list, in the second list, therefore interpreting the second list as an association list:Now, if you absolutely need to change the value of the
xglobal variable, simply do something like this:Notice how I avoided completely the need to modify
xinside the procedure, since in general that’s not the Scheme way to solve problems. In the end, the value inxis: