help, relationship transition lisp

Discussion of Common Lisp
Post Reply
marckup
Posts: 2
Joined: Thu Jun 16, 2011 1:52 am

help, relationship transition lisp

Post by marckup » Thu Jun 16, 2011 2:19 am

Hello,
are new to lisp and I have a program (written in Lisp) that must compare pairs of lists and find the transitivity relationship between pairs of elements. Below shows an example
mathematics: a = b, b = c, c = d, d = e, so results (according to the rules of mathematics) a = e
in lisp is so
((A B) (B C) (C D) (D E))

so the result would be (A E)

program and the trace listing here:
(setq L '((A B) (B C) (C D) (D E)))
(defun fun1 (L1 L2)
(cond
((and (null L1) (null L2)) nil)
((equalp (cdr L1) (car L2)) (cons (car L1) (cdr L2)))
(t (cons (car L1) (cdr L2)))
)
)


(defun fun3 (L &optional temp)
(cond
((null L) nil)
((cons (fun1 (car L) (cadr L)) (cdr (cdr L))) (fun3 (cons (fun1 (car L) (cadr L)) (cdr (cdr L)))))
(t (fun3 (cdr L) (cons (fun1 (car L) (cadr L)) (cdr (cdr L)))))
)
)



(trace fun3)
(fun3 L)
Here are the trace results.

Type :h and hit Enter for context help.
[1]>
FUN1
[2]>
((A B) (B C) (C D) (D E))
[3]>
FUN3
[4]>
;; Tracing function FUN3.
(FUN3)
[5]>
1. Trace: (FUN3 '((A B) (B C) (C D) (D E)))
2. Trace: (FUN3 '((A C) (C D) (D E)))
3. Trace: (FUN3 '((A D) (D E)))
4. Trace: (FUN3 '((A E)))
5. Trace: (FUN3 '((A)))
6. Trace: (FUN3 '((A)))
7. Trace: (FUN3 '((A)))
*****************************
2190. Trace: (FUN3 '((A)))
2191. Trace: (FUN3 '((A)))
2192. Trace: (FUN3 '((A)))
2193. Trace: (FUN3 '((A)))
2194. Trace: (FUN3 '((A)))
*** - Program stack overflow. RESET
[6]>
what we geasit please listig corectatimi mistake.

Thank you in advance

ps. sorry for bad English
 

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: help, relationship transition lisp

Post by marcoxa » Thu Jun 16, 2011 8:30 am

Ok... but what should the result be in case your input were ((A B) (B C) (C D) (H I) (I J))?

MA
Marco Antoniotti

marckup
Posts: 2
Joined: Thu Jun 16, 2011 1:52 am

Re: help, relationship transition lisp

Post by marckup » Thu Jun 16, 2011 10:01 am

marcoxa wrote:Ok... but what should the result be in case your input were ((A B) (B C) (C D) (H I) (I J))?

MA

in this case the result would be (AD) (HJ), ie A = D and H = J

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: help, relationship transition lisp

Post by marcoxa » Fri Jun 17, 2011 4:25 am

marckup wrote:
marcoxa wrote:Ok... but what should the result be in case your input were ((A B) (B C) (C D) (H I) (I J))?

MA

in this case the result would be (AD) (HJ), ie A = D and H = J
You mean ((A D) (H J)) don't you?

MA
Marco Antoniotti

Post Reply