Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am new to lisp, can someone tell me why my recursion call does not perform DFS. It is a variation of the pitcher problem:

I am new to lisp, can someone tell me why my recursion call does not perform DFS.

It is a variation of the pitcher problem: 3 jugs 16 9 7, and you have to manipulate them to get to ( 8 8 0) from (16 0 0)

Thank you.

//function to set nth value

(defun set-nth (list n val) (if (> n 0) (cons (car list) (set-nth (cdr list) (1- n) val)) (cons val (cdr list))))

(defun inList(l test) ;nothing more to check - return nil - no inner lists (if (null l) nil ;the first element of the list is a list? (if (equal (car l) test) ;if yes - return true t ;otherwise - try for the cdr of the list (inList (cdr l) test)))) (defvar start (list 16 0 0)) (defvar goal (list 8 0 0))

//the dfs recursion problem

(defun pitcher_problem (start goal visited) (cond ((equal start goal) (setq visited (cons start visited)) (print "Goal reached") (print (reverse visited))) ((inList visited start) nil) ((> (list-length visited) 20) nil) ((and (>= (+ (car start) (car(cdr start))) 9) (not (eql (car(cdr start)) 9))) (setq visited (cons start visited)) (print visited) (setq start (set-nth start 0 (- (car start) (- 9 (car(cdr start)))))) (setq start (set-nth start 1 9)) (print start) (pitcher_problem start goal visited))

((and (>= (+ (car start) (car(cdr(cdr start)))) 7) (not (eql (car(cdr(cdr start))) 7))) (setq visited (cons start visited)) (setq start (set-nth start 0 (- (car start) (- 7 (car(cdr (cdr start))))))) (setq start (set-nth start 2 7)) (pitcher_problem start goal visited)) ((< (+ (car start) (car(cdr start))) 16) (print start) (setq visited (cons start visited)) (setq start (set-nth start 0 (+ (car start) (car(cdr start))))) (setq start (set-nth start 1 0)) (pitcher_problem start goal visited))

((< (+ (car start) (car(cdr start))) 16) (setq visited (cons start visited)) (setq start (set-nth start 0 (+ (car start) (car(cdr(cdr start)))))) (setq start (set-nth start 2 0)) (pitcher_problem start goal visited))

((< (+ (car(cdr start)) (car(cdr(cdr start)))) 7) (setq visited (cons start visited)) (setq start (set-nth start 2 (+ (car(cdr start)) (car(cdr(cdr start)))))) (setq start (set-nth start 1 0)) (pitcher_problem start goal visited))

((< (+ (car(cdr start)) (car(cdr(cdr start)))) 9) (setq visited (cons start visited)) (setq start (set-nth start 1 (+ (car(cdr start)) (car(cdr(cdr start)))))) (setq start (set-nth start 2 0)) (pitcher_problem start goal visited))

((and (>= (+ (car (cdr start)) (car(cdr (cdr start)))) 7) (not (eql (car(cdr(cdr start))) 7))) (setq visited (cons start visited)) (setq start (set-nth start 1 (- (car(cdr start)) (- 7 (car(cdr(cdr start))))))) (setq start (set-nth start 2 7)) (pitcher_problem start goal visited))

((and (>= (+ (car (cdr start)) (car(cdr (cdr start)))) 9) (not (eql (car(cdr start))))) (setq visited (cons start visited)) (setq start (set-nth start 2 (- (car(cdr(cdr start))) (- 9 (car(cdr start)))))) (setq start (set-nth start 1 9)) (pitcher_problem start goal visited))

((and (>= (+ (car start) (car(cdr start))) 16) (not (eql (car start) 16))) (setq visited (cons start visited)) (setq start (set-nth start 1 (- (car (cdr start)) (- 16 (car start))))) (setq start (set-nth start 0 16)) (pitcher_problem start goal visited))

((and (>= (+ (car start) (car(cdr (cdr start)))) 16) (not (eql (car start) 16))) (setq visited (cons start visited)) (setq start (set-nth start 2 (- (car start) (- 16 (car(cdr(cdr start))))))) (setq start (set-nth start 0 16)) (pitcher_problem start goal visited))

)

)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

Bachelors degree in Information Systems or Statistics

Answered: 1 week ago