Hey, I need help defining a start function that will be used to run the
main function I have created for a problem, full of smaller functions. I
will input all of my code as you will probably not get it. It is basically
a problem to get three missionaries and three cannibals across a river
without any missionaries being eaten when the number of cannibals are more
than the missionaries.
Here is my code:
(defun make-state (b m c) (list T 3 3))
(defun boat-side (state) (nth 0 state))
(defun missionaries-leftside (state) (nth 1 state))
(defun cannibals-leftside (state) (nth 2 state))
(defun missionaries-rightside (state) (- 3 (nth 1 state)))
(defun cannibals-rightside (state) (- 3 (nth 2 state)))
(defun safe (state)
(cond (( < (missionaries-leftside state) (cannibals-leftside
state)) nil)
(( > 3 (missionaries-leftside state)) nil)
(( > 3 (cannibals-leftside state)) nil)
(( < 0 (missionaries-leftside state)) nil)
(( < 0 (cannibals-leftside state)) nil)
(( = 0 (missionaries-leftside state)) t)
(( < (missionaries-rightside state)
(cannibals-rightside state)) nil)
(( > 3 (missionaries-rightside state)) nil)
(( > 3 (cannibals-rightside state)) nil)
(( < 0 (missionaries-rightside state)) nil)
(( < 0 (cannibals-rightside state)) nil)
(( = 0 (missionaries-rightside state)) t)
(t state)))
(defun opposite (state num)
(cond ((>= (- state num ) 0) (- state num))
(t nil)))
(defun MC (state)
(safe (make-state (opposite (boat-side state))
(opposite (missionaries-leftside state)
1)
(opposite (cannibals-leftside state) 1))
(missionaries-rightside state)
(cannibals-rightside state))
(t nil))
(defun MM (state)
(safe (make-state (opposite (boat-side state))
(opposite (missionaries-leftside state)
2)
(cannibals-leftside state))
(missionaries-rightside state)
(cannibals-rightside state))
(t nil))
(defun CC (state)
(safe (make-state (opposite (boat-side state))
(missionaries-leftside state)
(opposite (cannibals-leftside state) 2))
(missionaires-rightside state)
(cannibals-rightside state))
(t nil))
(defun M (state)
(safe (make-state (opposite (boat-side state))
(opposite (missionaries-leftside state)
1)
(cannibals-leftside state))
(missionaries-rightside state)
(cannibals-rightside state))
(t nil))
(defun C (state)
(safe (make-state (opposite (boat-side state))
(missionaries-leftside state)
(opposite (cannibals-leftside state) 1))
(missionaries-rightside state)
(cannibals-rightside state))
(t nil))
(defun path (state goal been-list)
(cond ((null state) nil)
((equal state goal) (reverse (cons state
been-list)))
((not (member state been-list :test #'equal))
(or (path (MC state) goal (cons state
been-list))
(path (MM state) goal (cons state
been-list))
(path (CC state) goal (cons state
been-list))
(path (M state) goal (cons state
been-list))
(path (C state) goal (cons state
been-list))))))
(defun solve-problem (state goal) (path state goal nil))
I know it probably looks messy. Anyway...the start functions is supposed
to run the solve-problem function and produce a list of the states from
the start state (T 3 3) (t being a boat or trip..all explained in the
code) to the end state (nil 0 0) where there are no more trips. This is
states from the left-side. Therefore it is supposed to look like:
(defun start ()...)
that produces something like:
(T 3 3) (T 2 3) (T 1 2)...(nil 0 0). I hope this makes sense, and I have
asked for help elsewhere but still waiting. I don't want to be treated
like an idiot either.
If you could help that would great. Thanx.
"kazzarazza003" <········@students.unisa.edu.au> wrote in message news:<································@localhost.talkaboutprogramming.com>...
> Hey, I need help defining a start function that will be used to run the
> main function I have created for a problem, full of smaller functions.
[...]
> (defun solve-problem (state goal) (path state goal nil))
>
> I know it probably looks messy. Anyway...the start functions is supposed
> to run the solve-problem function and produce a list of the states from
> the start state (T 3 3) (t being a boat or trip..all explained in the
> code) to the end state (nil 0 0) where there are no more trips. This is
> states from the left-side. Therefore it is supposed to look like:
Just call solve-problem at the end of the file (solve-problem '(T 3 3) '(nil 0 0)).
"kazzarazza003" <········@students.unisa.edu.au> wrote in message news:<································@localhost.talkaboutprogramming.com>...
> Hey, I need help defining a start function that will be used to run the
> main function I have created for a problem, full of smaller functions. I
> will input all of my code as you will probably not get it. It is basically
> a problem to get three missionaries and three cannibals across a river
> without any missionaries being eaten when the number of cannibals are more
> than the missionaries.
>
> Here is my code:
>
> (defun make-state (b m c) (list T 3 3))
[cut to save space]
> (defun solve-problem (state goal) (path state goal nil))
>
> I know it probably looks messy. Anyway...the start functions is supposed
> to run the solve-problem function and produce a list of the states from
> the start state (T 3 3) (t being a boat or trip..all explained in the
> code) to the end state (nil 0 0) where there are no more trips. This is
> states from the left-side. Therefore it is supposed to look like:
>
> (defun start ()...)
>
> that produces something like:
>
> (T 3 3) (T 2 3) (T 1 2)...(nil 0 0).
Does this work?
(defun start ()
(solve-problem (list t 3 3) (list nil 0 0)))
By the way, I wonder if your make-state function has a bug. It always
returns the initial state, no matter what the arguments are.