From: =?KOI8-U?B?9MHSwdM=?=
Subject: How to re-write code without using setq?
Date: 
Message-ID: <98f3d2a6-fee3-4284-b8fa-5470a0df614a@p35g2000prm.googlegroups.com>
here is code,which is looking for the path in labirinth

(defun search_room (point
labirint)
  (cond ((null labirint)
nil)
           ((eql (caar labirint) point) (second (car
labirint)))
           (t (search_room point (cdr labirint)))))
(setq way '(in))
(defun search_path
(labirint)
  (setq way (cons (search_room(first way) labirint) way))   ;(push
(search_room(first  *way-out*) labirint)  *way-
out*)
     (cons (eql (car way)
'out)
         (t (reverse way)))     ;ÐÅÒÅ×ÁÒÁÞÉ×ÁÅÔ Á ÐÏÔÏÍ ÒÁÚÒÕÛÁÅÔ
ÓÐÉÓÏË
way
         (search_path labirint))

Plz help me to re-write it without usnig setq

From: Barry Margolin
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <barmar-7DE792.23062714112008@mara100-84.onlink.net>
In article 
<····································@p35g2000prm.googlegroups.com>,
 ????? <······@gmail.com> wrote:

> here is code,which is looking for the path in labirinth
> 
> (defun search_room (point
> labirint)
>   (cond ((null labirint)
> nil)
>            ((eql (caar labirint) point) (second (car
> labirint)))
>            (t (search_room point (cdr labirint)))))
> (setq way '(in))
> (defun search_path
> (labirint)
>   (setq way (cons (search_room(first way) labirint) way))   ;(push
> (search_room(first  *way-out*) labirint)  *way-
> out*)
>      (cons (eql (car way)
> 'out)
>          (t (reverse way)))     ;�������������� � ����� ���������
> ������
> way
>          (search_path labirint))
> 
> Plz help me to re-write it without usnig setq

Please post again with proper indentation so we can understand what you 
wrote.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Kaz Kylheku
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <20081114232308.235@gmail.com>
On 2008-11-14, =?KOI8-U?B?9MHSwdM=?= <······@gmail.com> wrote:
> Plz help me to re-write it without usnig setq

s/setq/setf/g
From: terance
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <d7a8e573-061d-4212-ad14-3224d2ca2df0@t39g2000prh.googlegroups.com>
here is formatted code


defun search_room (point labirint)
  (cond ((null labirint) nil)
           ((eql (caar labirint) point) (second (car labirint)))
           (t (search_room point (cdr labirint)))))

(setq way '(in))

(defun search_path (labirint)
  (setq way (cons (search_room(first way) labirint) way))
     (cons (eql (car way) 'out)
         (t (reverse way)))
         (search_path labirint))

in addition I'd like to know how to organise data-structure "stack"
using only cons/append/car/cdr/eql/cond/?
From: Barry Margolin
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <barmar-4A2D94.07375015112008@mara100-84.onlink.net>
In article 
<····································@t39g2000prh.googlegroups.com>,
 terance <······@gmail.com> wrote:

> here is formatted code
> 
> 
> defun search_room (point labirint)
>   (cond ((null labirint) nil)
>            ((eql (caar labirint) point) (second (car labirint)))
>            (t (search_room point (cdr labirint)))))

This could be rewritten:

(defun search_room (point labirint)
  (second (find point labirint :key #'car)))

> 
> (setq way '(in))

Global variables should be declared with DEFVAR.  And it's conventional 
to use names surrounded with *...* so that they stand out.

(defvar *way* '(in))

> 
> (defun search_path (labirint)
>   (setq way (cons (search_room (first way) labirint)
>                   way))
>   (cons (eql (car way) 'out)
>         (t (reverse way)))
>   (search_path labirint))

I can't figure out what thisfunction is supposed to be doing.  It calls 
CONS in the middle but doesn't do anything with the result, and it tries 
to call a function named T; it looks like it should be COND, but it's 
missing the consequence of (eql (car way) 'out).  At the end it calls 
itself unconditionally, so this should cause infinite recursion.

There's no way that function can possibly work.  Get it working first, 
then ask for advice on how to improve it.

> in addition I'd like to know how to organise data-structure "stack"
> using only cons/append/car/cdr/eql/cond/?

The general answer is that you pass the stack as a parameter in the 
recursive call, rather than using a global variable, so your function 
might have a structure something like this:

(defun search_path (labirint way)
  (let ((new-way ...))
    ...
    (search_path labirint new-way)))

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: terance
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <f32d5adf-11af-4edf-affc-34ed234a119c@a29g2000pra.googlegroups.com>
sorry for posting my wrong code really it does nothing.....this
posted
(finally))))) code is working.Well actually it finds the path from
the
labirint.The labirint is the list of linked rooms
e.x. ('((1 2) (2 4) (in 1) (in 3) (1 3) (4 5) (5 out)))[here is the
room with number "1" is connected with with the room with number "2",
so they are form one unit (1 2)] the result of algorithm's working is
(in 1 2 4 5 out)
(defun find_room (point
labirint)
  (cond ((null labirint) nil)
           ((eql (caar labirint) point) (second (car labirint)))
           (t (find_room point (cdr labirint)))))
(setq way '(in))
(defun find_path (labirint)
  (push (find_room (first way) labirint) way)
     (if (eql (first way)'out)
         (nreverse way)
   (find_path labirint)))
From: terance
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <b28ee112-bd97-4285-8552-2537e91628f2@a29g2000pra.googlegroups.com>
 sorry for posting my wrong code really it does nothing.....this
 posted (finally))))) code is working.Well actually it finds the path
from
 the labirint.The labirint is the list of linked rooms
 e.x. ('((1 2) (2 4) (in 1) (in 3) (1 3) (4 5) (5 out)))[here is the
 room with number "1" is connected with with the room with number "2",
  so they are form one unit (1 2)] the result of algorithm's working
is
 (in 1 2 4 5 out)
 (defun find_room (point labirint)
   (cond ((null labirint) nil)
            ((eql (caar labirint) point) (second (car labirint)))
            (t (find_room point (cdr labirint)))))
 (setq way '(in))
 (defun find_path (labirint)
   (push (find_room (first way) labirint) way)
      (if (eql (first way)'out)
          (nreverse way)
    (find_path labirint)))
From: William James
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <e317c9a1-1f2d-40cb-a38c-d5492be2eddc@a29g2000pra.googlegroups.com>
On Nov 15, 9:58 am, terance <······@gmail.com> wrote:
>  sorry for posting my wrong code really it does nothing.....this
>  posted (finally))))) code is working.Well actually it finds the path
> from
>  the labirint.The labirint is the list of linked rooms
>  e.x. ('((1 2) (2 4) (in 1) (in 3) (1 3) (4 5) (5 out)))[here is the
>  room with number "1" is connected with with the room with number "2",
>   so they are form one unit (1 2)] the result of algorithm's working
> is
>  (in 1 2 4 5 out)
>  (defun find_room (point labirint)
>    (cond ((null labirint) nil)
>             ((eql (caar labirint) point) (second (car labirint)))
>             (t (find_room point (cdr labirint)))))
>  (setq way '(in))
>  (defun find_path (labirint)
>    (push (find_room (first way) labirint) way)
>       (if (eql (first way)'out)
>           (nreverse way)
>     (find_path labirint)))

You need backtracking.  What if the labyrinth
list were expressed in this way?
((1 3) (2 4) (in 1) (in 3) (1 2) (4 5) (5 out))
From: Pascal J. Bourguignon
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <87skprjlhd.fsf@hubble.informatimago.com>
terance <······@gmail.com> writes:

>  sorry for posting my wrong code really it does nothing.....this
>  posted (finally))))) code is working.Well actually it finds the path
> from
>  the labirint.The labirint is the list of linked rooms
>  e.x. ('((1 2) (2 4) (in 1) (in 3) (1 3) (4 5) (5 out)))[here is the
>  room with number "1" is connected with with the room with number "2",
>   so they are form one unit (1 2)] the result of algorithm's working
> is
>  (in 1 2 4 5 out)
>  (defun find_room (point labirint)
>  � (cond ((null labirint) nil)
>  � � � � � �((eql (caar labirint) point) (second (car labirint)))
>  � � � � � �(t (find_room point (cdr labirint)))))
>  (setq way '(in))
>  (defun find_path (labirint)
>  � (push (find_room (first way) labirint) way)
>  � � �(if (eql (first way)'out)
>  � � � � �(nreverse way)
>  � �(find_path labirint)))

You do it again, posting unformatted code!



First, take note that:

1)  the character - is much easier to type than the character _
    (since the former is a single keypress, while the other is a choard,
     at least on the keyboard layouts I know).

2)  in all the natural languages I know, compound-words are  joined
    with a character -, not _.

3)  it is lisp style to use - instead of _ or CamelCase in identifiers.


So if you're in the same position as me, that of an occidental lisp
programmer using English or some other latin derived language, perhaps
you could adopt the same style here.



Now, let's discuss the most important part: you should use abstractions!

What would be the notion of a CAAR of a labyrinth?



A laryrinth has rooms, and doors to connect two rooms together.  When
you go thru one door, you pass from one room to the other.  We will
consider only both-way doors (but there may be sometimes one-way door
mechanisms).



So you should define functions such as:

(make-room name) --> room

(name room) --> name



(make-door room-a room-b) --> door

(rooms door) --> list of rooms ; returns the list of rooms this door leads to.

(go-thru room door) --> room ; return the room on the other side of the door, 
                             ; from the given room.



(make-labyrinth) --> labyrinth ; returns an empty labyrinth.

(doors labyrinth) --> list of doors ; returns the list of all the doors in the labyrinth.

(rooms labyrinth) --> list of rooms ; returns the list of all the rooms in the labyrinth.

(add-room labyrinth room) --> labyrinth ; returns a new labyrinth just like the original,
                                        ; but with a new room.

(add-door labyrinth door) -> labyrinth  ; returns a new labyrinth just like the original,
                                        ; but with a new door between two rooms of the
                                        ; labyrinth.

(find-path labyrinth from-room to-room) --> list of rooms




Finding a path in a labyrinth is actually a generic problem of
searching in a graph.  There are several graph search algorithms.  You
will have to choose one.  Let's say we use the breadth first search
algorithm.  This search algorithm is not specific to labyrinths, you
can use it for a lot of other problems.  So in implementing this
algorith, we won't refer directly the labyrinth, but a graph
abstraction.  LABYRINTH:FIND-PATH will map the labyrinth abstraction
to the graph abstraction.


To implement the breadth-first-search algorithm, we will need two
other abstract data types: fifo queues and maps:

(make-queue) --> queue
(enqueue queue element) --> queue
(queue-empty-p queue) --> boolean
(queue-head queue) --> element
(dequeue queue) --> queue, element

    (queue-empty-p (make-queue))
    (not (queue-empty-p (enqueue q e)))
    (queue-empty-p (dequeue (enqueue (make-queue) e)))
    (queue-head (enqueue (make-queue) e)) == e


(make-map) --> map
(map-enter map key value) --> map
(map-get map key) --> value

    (map-get (map-enter k v) k) == v
    (map-get (map-enter k v2 (map-enter k v1)) k) == v2




(defun breadth-first-search (target adjacent-vertices vertices props)
  ;; props maps: vertex -> (walkedp . reversed-path)
  ;; vertices is a queue of vertices be processed.
  ;; adjacent-vertices is a function taking a vertex and returning a list of adjacent vertices.
  ;; Returns a path to the target vertex.
  (if (queue-empty-p vertices)
      nil
      (let* ((current  (queue-head vertices))
             (vertices (dequeue    vertices))
             (prop     (map-get props current)))
        (cond
          ((eql current target) (reverse (cdr prop)))
          ((car prop) (breadth-first-search target adjacent-vertices vertices props))
          (t
           (let ((props (map-enter props current (cons t (cdr prop)))))
             (destructuring-bind (vertices props)
                 (reduce
                  (lambda (vertices-props adjacent-vertex)
                    (destructuring-bind (vertices props) vertices-props
                      (if (car (map-get props adjacent-vertex))
                          vertices-props
                          (list (enqueue vertices adjacent-vertex)
                                (map-enter props adjacent-vertex
                                           (cons nil (cons adjacent-vertex (cdr prop))))))))
                  (funcall adjacent-vertices current)
                  :initial-value (list vertices props))
               (breadth-first-search target adjacent-vertices vertices props))))))))



Then you can easily implement LABYRINTH:FIND-PATH as:

(defun find-path (labyrinth from to)
  (assert (member from (rooms labyrinth)))
  (assert (member to   (rooms labyrinth)))
  (breadth-first-search to
                        (lambda (room)
                          (mapcar (lambda (door) (go-thru room door))
                                  (remove room  (doors labyrinth)
                                          :test-not (function member)
                                          :key (function rooms))))
                        (enqueue (make-queue) from)
                        (map-enter (make-map) from (cons nil (list from)))))




-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defpackage "QUEUE"
  (:use "COMMON-LISP")
  (:export "MAKE-QUEUE" "ENQUEUE" "QUEUE-EMPTY-P" "QUEUE-HEAD"  "DEQUEUE"))

;; (make-queue) --> queue
;; (enqueue queue element) --> queue
;; (queue-empty-p queue) --> boolean
;; (queue-head queue) --> element
;; (dequeue queue) --> queue, element
;; 
;; (queue-empty-p (make-queue))
;; (not (queue-empty-p (enqueue q e)))
;; (queue-empty-p (dequeue (enqueue (make-queue) e)))
;; (queue-head (enqueue (make-queue) e)) == e



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defpackage "MAP"
  (:use "COMMON-LISP")
  (:export "MAKE-MAP" "MAP-ENTER" "MAP-GET"))

;; (make-map) --> map
;; (map-enter map key value) --> map
;; (map-get map key) --> value
;;
;; (map-get (map-enter k v) k) == v
;; (map-get (map-enter k v2 (map-enter k v1)) k) == v2


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defpackage "GRAPH"
  (:use "COMMON-LISP" "QUEUE" "MAP")
  (:export "BREADTH-FIRST-SEARCH"))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defpackage "LABYRINTH"
  (:use "COMMON-LISP"
        "GRAPH" "QUEUE" "MAP") 
  (:export
   "MAKE-LABYRINTH" "MAKE-ROOM" "MAKE-DOOR"
   "NAME" "DOORS" "ROOMS" "ADD-ROOM" "ADD-DOOR" "GO-THRU"
   "MAKE-LABYRINTH-WITH-ROOMS-AND-DOORS"
   "FIND-PATH"))

;; (make-room name) --> room
;; 
;; (name room) --> name
;; 
;; 
;; (make-door room-a room-b) --> door
;; 
;; (doors room) --> list of doors ; returns the list of doors connected to this room.
;; 
;; (go-thru room door) --> room ; return the room on the other side of the door, 
;;                              ; from the given room.
;; 
;; 
;; (make-labyrinth) --> labyrinth ; returns an empty labyrinth.
;; 
;; (doors labyrinth) --> list of doors ; returns the list of all the doors in the labyrinth.
;; 
;; (rooms labyrinth) --> list of rooms ; returns the list of all the rooms in the labyrinth.
;; 
;; (add-room labyrinth room) --> labyrinth ; returns a new labyrinth just like the original,
;;                                         ; but with a new room.
;; 
;; (add-door labyrinth door) -> labyrinth  ; returns a new labyrinth just like the original,
;;                                         ; but with a new door between two rooms of the
;;                                         ; labyrinth.



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defpackage "LABYRINTH-IMPLEMENTATION"
  (:nicknames "LABI")
  (:use "COMMON-LISP")
  (:shadow "ROOM")
  (:export "ROOM" "MAKE-ROOM" "ROOM-NAME" 
           "DOOR" "MAKE-DOOR" "DOOR-ROOMS"
           "LABY" "MAKE-LABY" "LABY-ROOMS" "LABY-DOORS"))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "QUEUE")

(defun make-queue    ()               (cons nil nil))
(defun enqueue       (queue element)  (cons (car queue) (cons element (cdr queue))))
(defun queue-empty-p (queue)          (not (or (car queue) (cdr queue))))
(defun queue-head    (queue)          (cond
                                        ((queue-empty-p queue)
                                         (error "Queue is empty"))
                                        ((car queue)
                                         (caar queue))
                                        (t
                                         (car (last (cdr queue))))))
(defun dequeue       (queue)          (cond
                                        ((queue-empty-p queue)
                                         (error "Queue is empty"))
                                        ((car queue)
                                         (values (cons (cdar queue) (cdr queue))
                                                 (caar queue)))
                                        (t
                                         (dequeue (cons (reverse (cdr queue)) nil)))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "MAP")

(defun make-map () '())
(defun map-enter (map key value) (acons key value map))
(defun map-get   (map key)       (cdr (assoc key map)))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "GRAPH")

(defun breadth-first-search (target adjacent-vertices vertices props)
  ;; props maps: vertex -> (walkedp . reversed-path)
  ;; vertices is a queue of vertices be processed.
  ;; adjacent-vertices is a function taking a vertex and returning a list of adjacent vertices.
  ;; Returns a path to the target vertex.
  (if (queue-empty-p vertices)
      nil
      (let* ((current  (queue-head vertices))
             (vertices (dequeue    vertices))
             (prop     (map-get props current)))
        (cond
          ((eql current target) (reverse (cdr prop)))
          ((car prop) (breadth-first-search target adjacent-vertices vertices props))
          (t
           (let ((props (map-enter props current (cons t (cdr prop)))))
             (destructuring-bind (vertices props)
                 (reduce
                  (lambda (vertices-props adjacent-vertex)
                    (destructuring-bind (vertices props) vertices-props
                      (if (car (map-get props adjacent-vertex))
                          vertices-props
                          (list (enqueue vertices adjacent-vertex)
                                (map-enter props adjacent-vertex
                                           (cons nil (cons adjacent-vertex (cdr prop))))))))
                  (funcall adjacent-vertices current)
                  :initial-value (list vertices props))
               (breadth-first-search target adjacent-vertices vertices props))))))))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "LABYRINTH-IMPLEMENTATION")

(defstruct room name)
(defstruct door rooms)
(defstruct laby rooms doors)





;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "LABYRINTH")

(defun make-labyrinth ()
  "returns a an empty labyrinth."
  (labi:make-laby))

(defun make-room (name)
  (labi:make-room :name name))

(defun make-door (room-a room-b)
  (labi:make-door :rooms (list room-a room-b)))


(defun name (room)
  (labi:room-name room))

(defun doors (labi)
  "returns the list of all the doors in the labyrinth or connected to the room."
  (labi:laby-doors labi))

(defgeneric rooms (self)
  (:method ((self labi:laby)) (labi:laby-rooms self))
  (:method ((self labi:door)) (labi:door-rooms self))
  (:documentation
   "returns the list of all the rooms in the labyrinth or connected to the door."))

(defun add-room (labyrinth room)
  "returns a new labyrinth just like the original, but with a new room."
  (if (member room (rooms labyrinth))
      labyrinth
      (labi:make-laby :rooms (cons room (rooms labyrinth))
                      :doors (doors labyrinth))))

(defun add-door (labyrinth door)
  "returns a new labyrinth just like the original, but with a new door
between two rooms of the labyrinth."
  (cond
    ((member door (doors labyrinth))
     labyrinth)
    ((every (lambda (room) (member room (rooms labyrinth))) (rooms door))
     (labi:make-laby :rooms (rooms labyrinth)
                     :doors (cons door (doors labyrinth))))
    (t
     (error "The door connects rooms not in this labyrinth"))))

(defun go-thru (room door)
  "return the room on the other side of the door,  from the given room."
  (assert (member room (rooms door)))
  (first  (remove room (rooms door))))



(defun make-labyrinth-with-rooms-and-doors (rooms doors)
  (reduce (function add-door) doors
          :initial-value (reduce (function add-room) rooms
                                 :initial-value (make-labyrinth))))




(defun find-path (labyrinth from to)
  (assert (member from (rooms labyrinth)))
  (assert (member to   (rooms labyrinth)))
  (breadth-first-search to
                        (lambda (room)
                          (mapcar (lambda (door) (go-thru room door))
                                  (remove room  (doors labyrinth)
                                          :test-not (function member)
                                          :key (function rooms))))
                        (enqueue (make-queue) from)
                        (map-enter (make-map) from (cons nil (list from)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "CL-USER")
(use-package "LABYRINTH")

(setf *print-circle* nil)
(let* ((labi (let* ((rin  (make-room 'in))
                    (rout (make-room 'out))
                    (r1   (make-room 1))
                    (r2   (make-room 2))
                    (r3   (make-room 3))
                    (r4   (make-room 4))
                    (r5   (make-room 5)))
               (make-labyrinth-with-rooms-and-doors
                (list rin rout r1 r2 r3 r4 r5)
                (list (make-door r1 r2) (make-door r2 r4) (make-door rin r1)
                      (make-door rin r3) (make-door r1 r3) (make-door r4 r5)
                      (make-door r5 rout)))))
       (rin  (find 'in  (rooms labi) :key (function name)))
       (rout (find 'out (rooms labi) :key (function name))))
  (print (mapcar (function name) (find-path labi rin rout)))) 



;;;; THE END ;;;;
From: Barry Fishman
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <m3vduovqfr.fsf@barry_fishman.acm.org>
terance <······@gmail.com> writes:

> sorry for posting my wrong code really it does nothing.....this posted
> (finally))))) code is working.Well actually it finds the path from the
> labirint.The labirint is the list of linked rooms
> e.x. ('((1 2) (2 4) (in 1) (in 3) (1 3) (4 5) (5 out)))[here is the
> room with number "1" is connected with with the room with number "2",
> so they are form one unit (1 2)] the result of algorithm's working is
> (in 1 2 4 5 out)
[Fixing format]
> (defun find_room (point labirint)
>   (cond ((null labirint) nil)
>         ((eql (caar labirint) point) (second (car labirint)))
>         (t (find_room point (cdr labirint)))))
>
> (setq way '(in))
> (defun find_path (labirint)
>   (push (find_room (first way) labirint) way)
>   (if (eql (first way) 'out)
>       (nreverse way)
>     (find_path labirint)))

Assuming this is meant to be Common Lisp, it is best to follow the
conventions of the language.  That means using dash rather than
underscore to build compound names, and spelling things correctly.
Although Common Lisp can sometimes do tail call optimization like
Scheme, it is usually best to use loops or mapping.

I like Barry Margolin's implementation of search_room better than yours,
but they both skip over the problem that there can be more than one
neighboring room.

(defun find-next-rooms (point labyrinth)
  (loop for room in labyrinth
        if (eql (car room) point)
        collect (cadr point)))

Also making WAY a global variable really violate any sense of scope.
WAY is produced by and the returned value of FIND-PATH.

One can (still ignoring the fact that FIND-ROOM only returns one value)
do something like:

(defun find-path (labyrinth)
  (loop for point = 'in then (find-room point labyrinth)
        while point
        collect point))

Or leaving things tail recursive (and avoiding another nested function):

(defun find-path (labyrinth &optional (path '(in)))
  (let ((room (find-room (car path) labyrinth)))
    (case room
      ((nil) nil)
      ((out) (nreverse (cons 'out path)))
      (t (find-path labyrinth (cons room path))))))

which allows for not finding a path.  But the code still needs to
backtrack and not just follow the first adjacent room found.

-- 
Barry Fishman
From: terance
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <497a30ac-097f-49b1-9872-26411f9fc546@b31g2000prb.googlegroups.com>
Plz explaine me how tore-write this code without using "&optional"-
construction?I want to make this code using only /cons/append/car/cdr/
eql/cond/.Sorry for making stupid posts, but actually I am studing
this lang for 5 days.
From: Rob Warnock
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <KaWdnVtSMI2Sm73UnZ2dnUVZ_tLinZ2d@speakeasy.net>
terance  <······@gmail.com> wrote:
+---------------
| Plz explaine me how tore-write this code without using "&optional"-
| construction?I want to make this code using only /cons/append/car/cdr/
| eql/cond/.Sorry for making stupid posts, but actually I am studing
| this lang for 5 days.
+---------------

Perhaps you could simply post your instructor's email address.
It's would make it much simpler for us to submit our work directly
to him/her. [Assuming he/she isn't already lurking here, that is...]


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: terance
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <2ace1a8d-0d2a-409f-8ef3-2a370a08113a@h23g2000prf.googlegroups.com>
On 16 ÎÏÑÂ, 14:37, ····@rpw3.org (Rob Warnock) wrote:
> terance š<······@gmail.com> wrote:
>
> +---------------
> | Plz explaine me how tore-write this code without using "&optional"-
> | construction?I want to make this code using only /cons/append/car/cdr/
> | eql/cond/.Sorry for making stupid posts, but actually I am studing
> | this lang for 5 days.
> +---------------
>
> Perhaps you could simply post your instructor's email address.
> It's would make it much simpler for us to submit our work directly
> to him/her. [Assuming he/she isn't already lurking here, that is...]
>
> -Rob
>
> -----
> Rob Warnock š š š š š š š š š š <····@rpw3.org>
> 627 26th Avenue š š š š š š š š <URL:http://rpw3.org/>
> San Mateo, CA 94403 š š š š š š (650)572-2607

······@gmail.com
From: Tamas K Papp
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <6oann7F2kg42U1@mid.individual.net>
On Sun, 16 Nov 2008 05:49:40 -0800, terance wrote:

> On 16 нояб, 14:37, ····@rpw3.org (Rob Warnock) wrote:
>> Perhaps you could simply post your instructor's email address. It's
>> would make it much simpler for us to submit our work directly to
>> him/her. [Assuming he/she isn't already lurking here, that is...]
>
> ······@gmail.com

That did not convince me.  You claim you are your own instructor, yet you 
are seeking to solve a very particular problem that is most likely an 
exercise.

However, you can demonstrate the truth of your claims by laying aside 
your labyrinth problem for a while, and starting to read a good Lisp book 
(PCL and ANSI Common Lisp come to mind) and learning the language 
gradually.  If you do that, I am sure your questions will be answered.

HTH,

Tamas
From: Patrick May
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <m2tza7y803.fsf@spe.com>
terance <······@gmail.com> writes:
> Plz explaine me how tore-write this code without using "&optional"-
> construction?I want to make this code using only /cons/append/car/cdr/
> eql/cond/.

     Sure, that's what they all say.  Then the next variation is "Okay,
now that you're coding in handcuffs, would you mind wearing this little
leather thing I happen to have laying around?"

     Lisp pornographers are the kinkiest.

Patrick

------------------------------------------------------------------------
S P Engineering, Inc.  | Large scale, mission-critical, distributed OO
                       | systems design and implementation.
http://www.spe.com/pjm | (C++, Java, Common Lisp, Jini, middleware, SOA)
From: Barry Fishman
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <m37i73a5zv.fsf@barry_fishman.acm.org>
terance <······@gmail.com> writes:
> Plz explaine me how tore-write this code without using "&optional"-
> construction?I want to make this code using only /cons/append/car/cdr/
> eql/cond/.Sorry for making stupid posts, but actually I am studing
> this lang for 5 days.

The use of optional is just to save writing another function.
[ Since I used nreverse I should never have used '(in)]

(defun find-path (labyrinth &optional (path (cons 'in nil)))
   ...)

Can be broken into:

(defun find-path-r (labyrinth path)
   ...)

(defun find-path (labrinth)
   (find-path-r labyrinth (cons 'in nil)))

It seems strange to want to tackle this problem with just
   cons/append/car/cdr/eql/cond

Before reaching a problem at this complexity it seem you would have
already written code for list/member/mapcar/mapcan/reverse/cadr etc.,
and it would be absurd not to reuse that code here.

I can't imagine coding lisp without let/apply/if/progn, but I can
certainly do without append.

-- 
Barry Fishman
From: Pascal J. Bourguignon
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <87ej1dklxo.fsf@hubble.informatimago.com>
Barry Margolin <······@alum.mit.edu> writes:

> In article 
> <····································@t39g2000prh.googlegroups.com>,
>  terance <······@gmail.com> wrote:
>
>> here is formatted code
>> 
>> 
>> defun search_room (point labirint)
>>   (cond ((null labirint) nil)
>>            ((eql (caar labirint) point) (second (car labirint)))
>>            (t (search_room point (cdr labirint)))))
>
> This could be rewritten:
>
> (defun search_room (point labirint)
>   (second (find point labirint :key #'car)))
>
>> 
>> (setq way '(in))
>
> Global variables should be declared with DEFVAR.  And it's conventional 
> to use names surrounded with *...* so that they stand out.
>
> (defvar *way* '(in))


Not at all.  It's conventional to use names surrounded with *...* for
a much more important reason: 

  because variable defined with DEFVAR and DEFPARAMETER are declared
  SPECIAL, and therefore any following binding of these variable will
  be a dynamic binding.  This changes the semantics dramatically.

Compare:

(mapc (function unintern) '(x *x*))
(defvar *x* 42)
(defun f () *x*)
(defun g (y) (let ((*x* y)) (f)))
(list (f) (g 0))
--> (42 0)

with:

(mapc (function unintern) '(x *x*))
(defvar *x* 42)
(defun f () *x*)
(defun g (y) (let ((x y)) (f)))
(list (f) (g 0))
--> (42 42)

and:

(mapc (function unintern) '(x *x*))
(defvar x 42)
(defun f () x)
(defun g (y) (let ((x y)) (f)))
(list (f) (g 0))
--> (42 0)


Notice how in the last case, when *...* are not used, the meaning of G
changed, while its source didn't.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
From: William James
Subject: Re: How to re-write code without using setq?
Date: 
Message-ID: <gfmvk1$tsu$1@aioe.org>
Pascal J. Bourguignon wrote:

> Barry Margolin <······@alum.mit.edu> writes:
> > Global variables should be declared with DEFVAR.  And it's
> > conventional to use names surrounded with *...* so that they stand
> > out.
> > 
> > (defvar way '(in))
> 
> Not at all.  It's conventional to use names surrounded with *...* for
> a much more important reason: 
> 
>   because variable defined with DEFVAR and DEFPARAMETER are declared
>   SPECIAL, and therefore any following binding of these variable will
>   be a dynamic binding.  This changes the semantics dramatically.
> 
> Compare:
> 
> (mapc (function unintern) '(x x))
> (defvar x 42)
> (defun f () x)
> (defun g (y) (let ((*x* y)) (f)))
> (list (f) (g 0))
> --> (42 0)
> 
> with:
> 
> (mapc (function unintern) '(x x))
> (defvar x 42)
> (defun f () x)
> (defun g (y) (let ((x y)) (f)))
> (list (f) (g 0))
> --> (42 42)
> 
> and:
> 
> (mapc (function unintern) '(x x))
> (defvar x 42)
> (defun f () x)
> (defun g (y) (let ((x y)) (f)))
> (list (f) (g 0))
> --> (42 0)
> 
> 
> Notice how in the last case, when *...* are not used, the meaning of G
> changed, while its source didn't.

Yes, even an expert could easily become befuddled when using
Cumbersome Lisp.

Therefore, don't use it.