sans-hubris
02-18-2001, 08:45 PM
Scheme sounds cool because it's all recursion (recursion is your friend). Does anyone have any sample code?
|
Click to See Complete Forum and Search --> : Scheme? sans-hubris 02-18-2001, 08:45 PM Scheme sounds cool because it's all recursion (recursion is your friend). Does anyone have any sample code? Strike 02-18-2001, 10:06 PM Here's something from my functional languages programming class: ;________________________________________ ;Revised version of the "queens" problem ;written by Danny DiPaolo, 11-1-99 ;Most of the functions were taken from the specific ;8 x 8 "queens" problem addressed in Springer and ;Friedman's text, but I have modified it to fit the ;specifications of Laboratory 14. It will now ;print all the solutions to a given boardsize if ;the function "queens" is applied to that number. ; ;Examples: ; ;> (queens 4) ;((2 4 1 3) (3 1 4 2)) ; ;> (queens 3) ;() ; ;> (length (queens 10)) ;724 ; ;I couldn't, however, think of an easier, more efficient ;way to test the validity of the solution than by testing ;the length of it. ;_________________________________________________ ____ ;This function tests to see if the next attempted move (try) ;is legal, given the list that has been constructed thus far ;(if any) - legal-pl (LEGAL PLacement list) ;N.B. - this function is an EXACT copy of the one from ;Springer and Friedman (define legal? (lambda (try legal-pl) (letrec ((good? (lambda (new-pl up down) (cond ((null? new-pl) #t) (else (let ((next-pos (car new-pl))) (and (not (= next-pos try)) (not (= next-pos up)) (not (= next-pos down)) (good? (cdr new-pl) (add1 up) (sub1 down))))))))) (good? legal-pl (add1 try) (sub1 try))))) ;_________________________________________________ ____ ;This function tests the length of the solution to ;see if we need to continue "cons"ing on more terms ;or not given to the specified board size. ; ;I modified this function so that it could test the ;validity of any solution for a given boardsize. (define solution? (lambda (legal-pl boardsize) (= (length legal-pl) boardsize))) ;_________________________________________________ ____ ;I had to modify this function so that it was passed ;the boardsize in its call, but other than that (and ;simply replacing "fresh-start" with boardsize), just ;about no changes were made. This function simply ;generates a solution. (define build-solution (lambda (legal-pl boardsize) (cond ((solution? legal-pl boardsize) legal-pl) (else (forward boardsize legal-pl boardsize))))) ;_________________________________________________ ____ ;This function dictates how the next solution will be ;chosen, as it is only called when the last solution ;was proven to be legal, and we are ready to try a new ;placement. ; ;I had to modify this function to include the boardsize ;as well, since it invokes "build-solution". (define forward (lambda (try legal-pl boardsize) (cond ((zero? try) (backtrack legal-pl boardsize)) ((legal? try legal-pl) (build-solution (cons try legal-pl) boardsize)) (else (forward (sub1 try) legal-pl boardsize))))) ;_________________________________________________ ____ ;This function is used when the last move is found to ;be unhelpful (although valid) - instead it tries another ;one until it finds a new solution. ; ;Again, I had to modify this function to include boardsize ;since it calls "forward", which has boardsize as a ;parameter due to the "build-solution" call within it (define backtrack (lambda (legal-pl boardsize) (cond ((null? legal-pl) '()) (else (forward (sub1 (car legal-pl)) (cdr legal-pl) boardsize))))) ;_________________________________________________ ____ ;This is pretty much the same function as the one in the book ;with just my minor "boardsize" tweaks, since build-solution ;is called. (define build-all-solutions (lambda (boardsize) (letrec ((loop (lambda (sol) (cond ((null? sol) '()) (else (cons sol (loop (backtrack sol boardsize)))))))) (loop (build-solution '() boardsize))))) ;_________________________________________________ ____ ;This function I made up entirely myself, and I only ;made it really to satisfy the syntactical limitations ;of the laboratory instructions. This makes it so that ;the input of "(queens 4)" will return a list of the ;two possible configurations that are valid solutions, ;even though my modifiend functions would return the same ;value by simply inputting "(build-all-solutions 4)". (define queens (lambda (n) (build-all-solutions n))) klamath 02-18-2001, 10:14 PM I'm learning Common LISP right now (which is similar to scheme). It's pretty damn cool... justlinux.com
Copyright Internet.com Inc. All Rights Reserved. |