Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Help with recursive functions

$
0
0
Hi, I have two recursive functions that are giving me trouble I was wondering if someone can take a look at them and help me. In some instances, it seems that it produces the correct result, in others they look wrong.

First function, zip (lst1 lst2)
takes item in first list, and pairs it with an the corresponding item in the second list. For example, (A B C) (1 2 3) produces: (a 1) (b 2) (c 3)


my code:

(defun zip (lst1 lst2)

(if (or (null lst1) (null lst2)) nil (cons (cons (car lst1) (car lst2)) (zip (cdr lst1) (cdr lst2))))





my input, its output, whats expected
in: (zip '(a b c) '(1 2 3)) out: ((A . 1) (B . 2) (C . 3)) exp: ((a 1) (b 2) (c 3))

in: (ZIP (A B C) (1 2 3)) out: ((A 1) ((B 2) ((C 3) NIL))) expected: I assume the right nil isnt supposed to be there

in: (ZIP (x y) (NIL ((A)))) out: ((x NIL) ((y ((A))) NIL)) exp: again, the right nil I imagine


Second function swap(lst):

essentially takes a list of lists that have 2 elements, each list within the list has its elements reversed. For example:
((1 2) (A B )) becomes: ((2 1)( B A))

(defun swap (lst)

(if (null lst) nil (cons(cons(cadar lst)(caar lst)) (swap(cdr lst)))) 
) 



input: (swap '((a B )/>/> (2 3) ("foo" "bar"))) out: (( B . A) (3 . 2) ("bar" . "foo")) expected: This seems right

in:(SWAP ((A B ) (2 3) (foo bar))) out: (( B . A) (3 . 2) (bar . foo)) exp: im not sure why this is considered wrong. It is a computer thats checking my work, so its looking for very precise answers

in:(SWAP (((A B ) (2 3)) (foo bar) (NIL NIL))) out: (((2 3) A B ) (bar . foo) (NIL)) exp: this is just obviously wrong

can someone help me out? How do I solve this? the fact that my own personal input seems right, and the computer program checking my work produces wrong input has my confused.


thanks

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>