This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

help, relationship transition lisp

4 posts · 1091 views

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

Re: help, relationship transition lisp

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

Re: help, relationship transition lisp

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

Re: help, relationship transition lisp

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