(define (enumerate-interval low high)
(if (> low high)
'()
(cons low (enumerate-interval (+ low 1) high))))
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define nil ())
(define (flatmap proc seq)
(accumulate append nil (map proc seq)))
(define (queens board-size)
(define (queen-cols k)
(if (= k 0) ; k = new # columns
(list empty-board) ; start with empty board
(filter
(lambda (positions) (safe? k positions)) ; weed out any positions in check
(flatmap ; create list of new boards with a queen added somewhere SAFE in column k
(lambda (rest-of-queens)
(map (lambda (new-row)
(adjoin-position new-row k rest-of-queens))
; adds new position (new-row, k): queen in column k...
(enumerate-interval 1 board-size)))
; ...for all rows.
(queen-cols (- k 1)))))) ; recursion/next iteration
(queen-cols board-size)) ; start first iteration
(define empty-board ())
(define (make-queen row col)
(cons row col))
(define (row-queen q)
(car q))
(define (col-queen q)
(cdr q))
; a board is just a list of queens.
(define (adjoin-position row col board)
(append board (list (make-queen row col))))
; extract the kth queen from the board
(define (queen-board j board)
(if (and (<= 1 j) (<= j (length board)))
(list-ref board (- j 1))
(error "queen-board out of range" j (length board))))
(define (safe? k board)
(define (iter j)
(cond
; final termination: didn't get checked by any of the other queens
((>= j k)
#t)
; early termination: check!!
((check-queens? (queen-board j board) (queen-board k board))
#f)
; next iteration
(else
(iter (+ j 1)))
)
)
(iter 1)
)
; returns true if q1 and q2 check each other.
(define (check-queens? q1 q2)
(let ( (r1 (row-queen q1)) (c1 (col-queen q1))
(r2 (row-queen q2)) (c2 (col-queen q2)))
(cond
((= r1 r2) #t) ; horizontal and vertical checks are easy
((= c1 c2) #t)
; diagonal check: |rise| = |run|
((= (abs (- r1 r2)) (abs (- c1 c2))) #t)
(else #f)
)
)
)
; testing code. gotta have board output...
(define (display-board board)
(define (display-space n)
(cond
((> n 0)
(display " ")
(display-space (- n 1)))
(else
"unused return value")))
(define (iter i)
(cond
((<= i (length board))
(let ( (q (queen-board i board)) )
(display-space (- (row-queen q) 1))
(display "Q")
(newline)
(iter (+ i 1)) ; really? it's in here??
)
)
(else
"unused return value")
)
)
(display "---------------\n")
(iter 1)
)
(define (test-2.42)
(define (test n)
(for-each
display-board
(queens n)
)
)
;(test 1) ; useful to make sure infrastructure is working
;(test 2) ; useful to check horizontal/vertical checks
;(test 3) ; there really ARE no solutions for n = 3! try disabling diagonal checks and you'll see.
;(test 4) ; just a parallelogram and its rotation
(test 5)
;(test 8) ; a little bit TOO much output (how do you scroll in this stupid window?)
(display "\nthat's all, folks!")
)
(test-2.42)
About Online Scheme Compiler
Try our Online Scheme Compiler (Version Gauche v0.9.10) to Edit, Run, and Share your Scheme Code directly from your browser. This online development environment provides you the latest version Gauche v0.9.10.
How to use Online Scheme Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Scheme Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Scheme Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Scheme Compiler: Save and Share Scheme Code (Project)
Save Scheme Project Online
You can save your Scheme Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Scheme Project Online
You can use this feature to share your Scheme Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Scheme Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Scheme Compiler
There are several benefits of using the Online Scheme Compiler to run your Scheme code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.