views:

71

answers:

1
(defun returnFirstCharacter(p)

(if (symbolp p) (char (symbol-name p) 0) nil)
)

 (defun returnRestCharacters (p)
(let ()
    (intern (subseq(string p) 1))
)         
)

 (defun val (x a)
  (cond ((null a) nil)
        ((equal (car(cdr a)) x) (cdr (cdar a)))
        (t (val x (cdr a)))
    )
 )



 (defun match (pattern data)
(cond 
    ((equal pattern data) t)
    ((match2 pattern data nil))
    (t nil)
)
 )

  (defun match2 (p d a)
(cond

    ( (null p)(cond 
                    ((null a) t)
                    (t a)
                )
    )

    ((numberp p) (cond 
                        ((equal p d) t) 
                        (t nil)
                    )
    ) 

    (  (symbolp p) (cond
                        ((alpha-char-p (returnFirstCharacter p))
                                    (let ()(if (equal p d) (match2 nil nil a) nil)))
                        ((equal (returnFirstCharacter p) #\=)
                                    (let ()
                                            (if (and  (returnRestCharacters p) (null (val (returnRestCharacters p) a)))
                                                (match2 nil nil (cons (
                                                                                list  = (returnRestCharacters p)  d)  a)
                                                )
                                                (let ()
                                                    (if (and  (returnRestCharacters p) (equal (val (returnRestCharacters p) a) (car d))) 
                                                        (match2 (cdr p) (cdr d) a) nil)
                                                )
                                            )
                                    )
                        )
                    )
    )
    (   (listp p) 
                    (cond 
                        ((equal (list-length p) (list-length d))
                            (let ()
                            (   if (equal p d) 
                                    (match2 nil nil a)  
                                    (let ()
                                        (append (match2 (car p) (car d) a)(match2 (cdr p) (cdr d) a) )
                                    )


                            )
                            )
                        )
                    )
    )

)
)

I have match2 being called twice by itself.I want the recursive calls to happen. the moment a call of match2 returns either true or nil the program is terminated i want control to be passed back to the previous match2 which called it.

As you can see I have called match2 twice. (listp p) this condiiton in match2 I need to do recursion.How to do this

+1  A: 

This looks like part of a COND block. With that assumption in mind, I've added the COND back in and re-formatted the code to make it a bit more obvious.

(cond ((alpha-char-p (returnFirstCharacter p))
       (let () (if (equal p d) (match2 nil nil a) nil)))
      ((equal (returnFirstCharacter p) #\=)
       (let ()
         (if (and (returnRestCharacters p)
                  (null (val (returnRestCharacters p) a)))
             (match2 nil nil (cons (list '= (returnRestCharacters p) d) a))
             (let ()
               (if (and (returnRestCharacters p) 
                        (equal (val (returnRestCharacters p) a)
                               (car d))) 
                   (match2 (cdr p) (cdr d) a) nil))))))

First off, you're calling MATCH2 from the tail position, so it is not obvious what you mean by "passing control back to the previous match2". Once a call to MATCH2 happens, as written the return value will simply be propagated up the call chain, since you're not doing anything with the return value.

Second, you're using empty LET blocks for no obvious reason (the first IF calls MATCH2 if the condition is true, otherwise slips to the next IF). In neither case do you have multiple expressions that need evaluating, so you could've just skipped the LET blocks. Furthermore, using PROGN is a bit more conventional than using an empty LET block (doesn't matter to the compiler, but it does matter to the human reader).

It would probably help if you replace the code segment above with the whole function definition of MATCH2 and added a "plain english" description of what you think it should do.

Edit: With the whole code published (and with atrocious formatting), there's only one case within MATCH2 that there is a concept of "return and do something" and that's the two calls that are arguments to APPEND, right at the end. There's at least one piece of code that is probably going to cause run-time errors (and definitely compile-time warnings), with = used as a variable (unless there's a dynamic inding elsewhere, that's probably supposed to be '=).

You still have not explained what the code is supposed to do. If you sit down and write what you expect the code to do, in a step-by-step fashion, it's possible that you'll figure out where you're going wrong.

Vatine
Please see the edited version.I have added full code and I know I need to understand Lisp but with assignment deadline of 2 days it's very difficult.Appreciate your help
gizgok