From: CHRISTOS TTINIOZOU
Subject: Pathfinder function ?????????
Date: 
Message-ID: <ePNz3M0d#GA.201@upnetnews03>
 I  want  to  implement a function that find the path of a list using car
and cdr commands. e.g we have a list

x = (A (D E) B C ( F G (H)))


F  =  (PATHFINDER X '(5 1))

HOW CAN I DO THAT !!!

THANKS .

From: Stig Hemmer
Subject: Re: Pathfinder function ?????????
Date: 
Message-ID: <ekvg16s6zz4.fsf@verden.pvv.ntnu.no>
"CHRISTOS TTINIOZOU" <··········@email.msn.com> writes:
>  I  want  to  implement a function that find the path of a list using car
> and cdr commands. e.g we have a list
> 
> x = (A (D E) B C ( F G (H)))
> 
> F  =  (PATHFINDER X '(5 1))

> HOW CAN I DO THAT !!!

Well, the straight-forward way would probably be simplest...

(PATHFINDER X '(5 1)) => (PATHFINDER (CDR X) '(4 1))
...
(PATHFINDER X '(1 1)) => (PATHFINDER (CAR X) '(1))
{Not the same X here, of course}                

(PATHFINDER X '()) => X
{Ditto}

Stig Hemmer,
Jack of a Few Trades.
From: Aaron Crane
Subject: Re: Pathfinder function ?????????
Date: 
Message-ID: <djg16s599r.fsf@planet.praeclarus.demon.co.uk>
In article <···············@upnetnews03>,
"CHRISTOS TTINIOZOU" <··········@email.msn.com> writes:
>  I  want  to  implement a function that find the path of a list using car
> and cdr commands. e.g we have a list
> 
> x = (A (D E) B C ( F G (H)))
> 
> F  =  (PATHFINDER X '(5 1))

I find this highly functional implementation exceptionally elegant, but it
doesn't use CAR or CDR anywhere.  The definitions of CURRY and COMPOSE are
due to Paul Graham.

    (defun curry (fn &rest values)
      #'(lambda (&rest args)
          (apply fn (append values args))))

    (defun compose (&rest fns)
      (destructuring-bind (fn1 . rest) (reverse fns)
        #'(lambda (&rest args)
            (reduce #'(lambda (v f) (funcall f v))
                    rest
                    :initial-value (apply fn1 args)))))

    (defun path-func (path)
      (apply #'compose
             (reverse (mapcar #'(lambda (x) (curry #'nth x)) path))))

    (defun pathfinder (list path)
      (funcall (path-func path) list))

It doesn't do quite the same thing -- the indices are (correctly, in my
view) zero-based rather than one-based.  If you *really* want one-based
indices, simply change PATH-FUNC to

    (defun path-func (path)
      (apply #'compose
             (reverse (mapcar #'(lambda (x) (curry #'nth (1- x))) path))))

Note also that the maximum path length is limited by CALL-ARGUMENTS-LIMIT.

-- 
Aaron Crane   <···········@pobox.com>   <URL:http://pobox.com/~aaronc/>
 ** Please send on-topic followups by Usenet, not email **
From: Christopher J. Vogt
Subject: Re: Pathfinder function ?????????
Date: 
Message-ID: <36FBEE58.5B0CB841@computer.org>
CHRISTOS TTINIOZOU wrote:
> 
>  I  want  to  implement a function that find the path of a list using car
> and cdr commands. e.g we have a list
> 
> x = (A (D E) B C ( F G (H)))
> 
> F  =  (PATHFINDER X '(5 1))
> 
> HOW CAN I DO THAT !!!
> 
> THANKS .

Sounds like homework.  Heres a hint:

(defun pathfinder (list path)
  (nth (1- (second path)) (nth (1- (first path)) list)))

This doesn't use just car and cdr, but you could write your own
version of nth using just car and cdr, call it my-nth.  You
can write it either recursively or as a loop.  In either case
it is only about 4 lines of code.