Guest User

Untitled

a guest
Mar 26th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 13.47 KB | None | 0 0
  1. (define (field? maze x y)
  2.   (if (= y 0)
  3.       (if (= x 0)
  4.           (caar maze)
  5.           (field? (cons (cdar maze) (cdr maze)) (- x 1) y))
  6.       (field? (cdr maze) x (- y 1))))
  7.  
  8. (define (turnleft state)
  9.   (define (change_left orient)
  10.     (cond
  11.       ((equal? orient 'north) 'west)
  12.       ((equal? orient 'east) 'north)
  13.       ((equal? orient 'south) 'east)
  14.       (else 'south)))
  15.   (list (car state) (cadr state) (change_left (caddr state))))
  16.  
  17. (define (new-maze maze x y value)
  18.   (define (new_maze maze x y value newmaze)
  19.     (if (= y 0)
  20.         (if (= x 0)
  21.             (cons (cons value (cdar maze)) (cdr maze))
  22.             (cons (cons (caar maze) (car (new_maze (cons (cdar maze) (cdr maze)) (- x 1) y value newmaze))) (cdr (new_maze (cons (cdar maze) (cdr maze)) (- x 1) y value newmaze))))
  23.         (cons (car maze) (new_maze (cdr maze) x (- y 1) value newmaze))))
  24.   (new_maze maze x y value '())
  25.   )        
  26.      
  27.  
  28. (define (putmark state)
  29.   (define (put_mark maze x y)
  30.     (new-maze maze x y (+ (field? maze x y) 1)))
  31.   (list (put_mark (car state) (caadr state) (cadadr state)) (cadr state) (caddr state)))
  32.  
  33. (define (getmark state)
  34.   (define (get_mark maze x y)
  35.     (if (> (field? maze x y) 0)
  36.         (new-maze maze x y (- (field? maze x y) 1))
  37.         (new-maze maze x y (field? maze x y) )
  38.         ))
  39.   (list (get_mark (car state) (caadr state) (cadadr state)) (cadr state) (caddr state)))
  40.  
  41. (define (north?? state)
  42.   (if (equal? (caddr state) 'north)
  43.       #t
  44.       #f))
  45.  
  46. (define (mark?? state)
  47.   (if (> (field? (car state) (caadr state) (cadadr state)) 0)
  48.       #t
  49.       #f))
  50.  
  51. (define (wall?? state)
  52.   (cond
  53.     ((equal? (caddr state) 'north) (equal? (field? (car state) (caadr state) (- (cadadr state) 1)) 'w))
  54.     ((equal? (caddr state) 'west) (equal? (field? (car state) (- (caadr state) 1) (cadadr state)) 'w))
  55.     ((equal? (caddr state) 'south) (equal? (field? (car state) (caadr state) (+ (cadadr state) 1)) 'w))
  56.     (else (equal? (field? (car state) (+ (caadr state) 1) (cadadr state)) 'w))))
  57.  
  58. (define (my-step state)
  59.   (if (not (wall?? state))
  60.       (cond
  61.         ((equal? (caddr state) 'north) (list (car state) (list (caadr state) (- (cadadr state) 1)) 'north))
  62.         ((equal? (caddr state) 'west) (list (car state) (list (- (caadr state) 1) (cadadr state)) 'west))
  63.         ((equal? (caddr state) 'south) (list (car state) (list (caadr state) (+ (cadadr state) 1)) 'south))
  64.         (else (list (car state) (list (+ (caadr state) 1) (cadadr state)) 'east)))
  65.       state
  66.       ))
  67.  
  68. (define (simulate state expr program limit threshold)
  69.  
  70.   (define actions 0)
  71.  
  72.   (define (inc-actions)
  73.     (set! actions (+ actions 1)))
  74.  
  75.   (define my-limit 0)
  76.  
  77.   (define (inc-limit)
  78.            (set! my-limit (+ my-limit 1)))
  79.  
  80.   (define (dec-limit)
  81.            (set! my-limit (- my-limit 1)))
  82.  
  83.   (define my-threshold 0)
  84.  
  85.   (define (dec-threshold)
  86.     (set! my-threshold (- my-threshold 1)))
  87.  
  88.   (define kill-me #f)
  89.  
  90.   (define (kill-me-now)
  91.     (set! kill-me #t))
  92.  
  93.   (define (my-simulate state expr program)
  94.    
  95.   (define (turn-left)
  96.     (inc-actions)
  97.     (dec-threshold)
  98.     (if (= my-threshold -1)
  99.         (kill-me-now))
  100.         (turnleft state)
  101.     )
  102.    
  103.   (define (put-mark)
  104.     (inc-actions)
  105.     (dec-threshold)
  106.     (if (= my-threshold -1)
  107.         (kill-me-now))
  108.     (putmark state)  
  109.     )
  110.    
  111.   (define (get-mark)
  112.     (cond
  113.     ((mark?? state) (inc-actions) (dec-threshold) (if (= my-threshold -1) (kill-me-now)) (getmark state))
  114.     ((not (mark?? state)) (kill-me-now) state)
  115.     )
  116.     )
  117.    
  118.   (define (step)
  119.     (cond
  120.       ((not (wall?? state)) (inc-actions) (dec-threshold) (if (= my-threshold -1) (kill-me-now)) (my-step state))
  121.       ((wall?? state) (kill-me-now) state)
  122.     )
  123.     )
  124.    
  125.   (define (wall?)
  126.     (wall?? state))
  127.    
  128.   (define (mark?)
  129.     (mark?? state))
  130.    
  131.   (define (north?)
  132.     (north?? state))
  133.    
  134.   (define (my-if expr)
  135.     (cond
  136.       ((equal? expr 'wall?) (wall?))
  137.       ((equal? expr 'mark?) (mark?))
  138.       (else (north?))))
  139.  
  140.    (define (execute-if procedure)
  141.      (if (my-if (cadr procedure))
  142.          (caddr procedure)
  143.          (cadddr procedure)))
  144.    
  145.   (define (find-procedure program procedure)
  146.         (if (not (null? program))
  147.             (cond
  148.               ((equal? (cadar program) procedure) 'procedure)
  149.                 (else (if (null? (cdr program))
  150.                     'not-procedure
  151.                     (find-procedure (cdr program) procedure)))
  152.                 )
  153.             'not-procedure
  154.         ))
  155.  
  156.    (define (find-procedure2 program procedure)
  157.         (if (not (null? program))
  158.             (cond
  159.               ((equal? (cadar program) procedure) (dec-limit) (list (caddar program) 'end-proc))
  160.                 (else (if (null? (cdr program))
  161.                     'not-procedure
  162.                     (find-procedure2 (cdr program) procedure)))
  163.                 )
  164.             'not-procedure
  165.         ))
  166.    
  167.   (define (execute-procedure procedure)
  168.     (if (not kill-me)
  169.         (if (= my-limit -1)
  170.             state
  171.             (if (list? procedure)
  172.                 (if (not (null? procedure))
  173.                     (if (equal? (car procedure) 'if)
  174.                         (my-simulate state (execute-if procedure) program)
  175.                         (my-simulate (execute-procedure (car procedure)) (cdr procedure) program))
  176.                     state)
  177.                 (if (equal? (find-procedure program procedure) 'procedure)
  178.                     (my-simulate (my-simulate state (find-procedure2 program procedure) program) '() program)
  179.                     (my-simulate state procedure program))
  180.                 )
  181.             )
  182.         state)
  183.     )
  184.  
  185.   (cond
  186.     ((equal? kill-me #t) state)
  187.     ((equal? expr 'end-proc) (inc-limit) state)
  188.     ((equal? expr '()) state)
  189.     ((equal? expr 'step) (step))
  190.     ((equal? expr 'turn-left) (turn-left))
  191.     ((equal? expr 'put-mark) (put-mark))
  192.     ((equal? expr 'get-mark) (get-mark))
  193.     (else (execute-procedure (if (not (equal? expr '())) expr '())))))
  194.  
  195.   (set! my-limit limit)
  196.   (set! my-threshold (cadddr threshold))
  197.  
  198.   (let ((end-state (my-simulate state expr program)))
  199.     (append (list actions) (list end-state)))
  200.    
  201.   )
  202.  
  203. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  204. ;MERGE SORT
  205.  
  206. (define split (lambda (s)
  207.                 (cond
  208.                   ((null? (cdr s)) (cons s '()))
  209.                   ((null? (cddr s)) (cons (list (car s)) (cdr s)))
  210.                   (else (let* (
  211.                                (ab (split (cddr s)))
  212.                                (a (car ab))
  213.                                (b (cdr ab))
  214.                               )
  215.                           (cons (cons (car s) a) (cons (cadr s) b))
  216.                           )))))
  217.  
  218. (define (merge a b)
  219.   (cond
  220.     ((null? a) b)
  221.     ((null? b) a)
  222.     ((comparator (car a) (car b)) (cons (car a) (merge (cdr a) b )))
  223.     (else (merge b a ))))
  224.  
  225. (define my-list
  226.   '(((18 7 6 20) ((procedure start (put-mark (if wall? turn-left step) start)))) ((8 5 5 2) ((procedure start (step step step put-mark))))))
  227.  
  228. (define (comparator a b)
  229.   (let ((value1 (car a)) (value2 (car b)))
  230.     (if (< (car value1) (car value2))
  231.         #t
  232.         (if (> (car value1) (car value2))
  233.             #f
  234.             (if (< (cadr value1) (cadr value2))
  235.                 #t
  236.                 (if (> (cadr value1) (cadr value2))
  237.                     #f
  238.                     (if (< (caddr value1) (caddr value2))
  239.                         #t
  240.                         (if (> (caddr value1) (caddr value2))
  241.                             #f
  242.                             (if (< (cadddr value1) (cadddr value2))
  243.                                 #t
  244.                                 (if (> (cadddr value1) (cadddr value2))
  245.                                     #f
  246.                                     #t
  247.                                     )
  248.                                 )
  249.                             )
  250.                         )
  251.                     )
  252.                 )
  253.         )
  254.     )
  255.   )
  256.   )
  257.  
  258. (define (merge-sort s)
  259.   (cond
  260.     ((null? s) s)
  261.     ((null? (cdr s)) s)
  262.     (else (let* (
  263.                  (ab (split s))
  264.                  (sa (merge-sort (car ab)))
  265.                  (sb (merge-sort (cdr ab)))
  266.                 )
  267.             (merge sa sb)))))
  268.  
  269. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  270.              
  271. (define (diff-row o d)
  272.   (if (null? (cdr o))
  273.       (if (equal? (car o) 'w)
  274.           0
  275.           (abs (- (car o) (car d))))
  276.       (+ (if (equal? (car o) 'w) 0 (abs (- (car o) (car d)))) (diff-row (cdr o) (cdr d)))
  277.       ))
  278.  
  279. (define (diff-of-fields output-maze desired-maze)
  280.   (if (null? (cdr output-maze))
  281.       (diff-row (car output-maze) (car desired-maze))
  282.       (+ (diff-row (car output-maze) (car desired-maze)) (diff-of-fields (cdr output-maze) (cdr desired-maze)))
  283.   ))
  284.  
  285. (define (man-dist output-state desired-state)
  286.   (if (equal? (caddr output-state) (caddr desired-state))
  287.       (+ (abs (- (caadr output-state) (caadr desired-state))) (abs (- (cadadr output-state) (cadadr desired-state))))
  288.       (+ (+ (abs (- (caadr output-state) (caadr desired-state))) (abs (- (cadadr output-state) (cadadr desired-state)))) 1))
  289.   )
  290.  
  291. (define (length-of-prg prg)
  292.   (if (null? prg)
  293.       0
  294.       (+ (length-of-proc (car prg)) (length-of-prg (cdr prg))))
  295.   )
  296.  
  297. (define (length-of-proc proc)
  298.   (if (null? proc)
  299.       0
  300.       (if (list? (car proc))
  301.           (+ (length-of-proc (car proc)) (length-of-proc (cdr proc)))
  302.           (if (or (equal? (car proc) 'procedure) (equal? (car proc) 'if))
  303.               (length-of-proc (cdr proc))
  304.               (+ (length-of-proc (cdr proc)) 1))
  305.           )
  306.       )
  307.   )
  308.  
  309. (define (return-value prg pair stack_size threshold)
  310.   (let ((output-state (cadr (simulate (caar pair) 'start prg stack_size threshold))) (desired-state (cadar pair)) (len (car (simulate (caar pair) 'start prg stack_size threshold))))
  311.               (list (diff-of-fields (car output-state) (car desired-state)) (man-dist output-state desired-state) (length-of-prg prg) len)
  312.               )
  313.   )
  314.  
  315. (define (return-final-value prg pairs stack_size threshold)
  316.   (if (null? (cdr pairs))
  317.       (return-value prg (list (car pairs)) stack_size threshold)
  318.       (let ((value1 (return-value prg (list (car pairs)) stack_size threshold)) (value2 (return-final-value prg (cdr pairs) stack_size threshold)))
  319.         (list (+ (car value1) (car value2)) (+ (cadr value1) (cadr value2)) (caddr value1) (+ (cadddr value1) (cadddr value2))))
  320.         )
  321.   )
  322.  
  323. (define (check-threshold value threshold)
  324.   (if (> (car value) (car threshold))
  325.       #f
  326.       (if (> (cadr value) (cadr threshold))
  327.           #f
  328.           (if (> (caddr value) (caddr threshold))
  329.               #f
  330.               (if (> (cadddr value) (cadddr threshold))
  331.                   #f
  332.                   #t
  333.                   )
  334.               )
  335.           )
  336.       )
  337.   )
  338.  
  339. (define (evaluate prgs pairs threshold stack_size)
  340.  
  341.   (define final-list '())
  342.  
  343.   (define (push o)
  344.     (let ((s final-list))
  345.       (set! final-list (cons o s))))
  346.  
  347.   (define (my-evaluate prgs pairs threshold stack_size)
  348.     (if (null? (cdr prgs))
  349.         (let ((final-value (return-final-value (car prgs) pairs stack_size threshold)))
  350.           (if (check-threshold final-value threshold)
  351.               (push (list final-value (car prgs)))
  352.               )
  353.         )
  354.         (cond
  355.           (else (my-evaluate (list (car prgs)) pairs threshold stack_size) (my-evaluate (cdr prgs) pairs threshold stack_size))
  356.           )
  357.     )
  358.     )
  359.   (cond
  360.     (else (my-evaluate prgs pairs threshold stack_size) (merge-sort final-list))
  361.   )
  362.   )
  363.  
  364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  365.  
  366.  
  367.  
  368. ;((w w w w w)   ((w w w w w)
  369.  ;(w 1 w 1 w)    (w 0 w 1 w)
  370.  ;(w 3 0 0 w)    (w 1 0 1 w)
  371.  ;(w w w w w))   (w w w w w))
  372.  
  373. (define get-maze
  374. '(
  375. (w w w w w w)
  376. (w 0 w 0 w w)
  377. (w 0 w 0 0 w)
  378. (w 0 0 0 w w)
  379. (w w w w w w)
  380. )
  381. )
  382.  
  383. (define right-hand-rule-prg
  384.   '(
  385.     (procedure start
  386.       ( turn-right
  387.         (if wall?
  388.            ( turn-left
  389.              (if wall?
  390.                  (turn-left
  391.                      (if wall?
  392.                         turn-left
  393.                         step
  394.                      )
  395.                  )
  396.                  step
  397.               )
  398.            )
  399.            step  
  400.         )
  401.         put-mark
  402.         start
  403.       )
  404.     )  
  405.     (procedure turn-right (turn-left turn-left turn-left))
  406.   )
  407. )
  408.  
  409. (define prgs
  410. '(
  411.    (
  412.       (procedure start
  413.          (turn-right (if wall? (turn-left
  414.              (if wall? (turn-left (if wall? turn-left step)) step)) step)
  415.                  put-mark start )
  416.       )  
  417.       (procedure turn-right (turn-left turn-left turn-left))
  418.   )
  419.   (
  420.       (procedure start  (put-mark (if wall? turn-left step) start))
  421.   )
  422.   (
  423.       (procedure start (step step step put-mark))
  424.   )
  425. )
  426. )
  427.  
  428. (define pairs
  429. '(
  430.   (
  431.    (((w w w w w w)
  432.      (w 0 w 0 w w)
  433.      (w 1 w 0 0 w)
  434.      (w 1 0 0 w w)
  435.      (w w w w w w))
  436.      (1 3) south)
  437.  
  438.    (((w w w w w w)
  439.      (w 0 w 0 w w)
  440.      (w 0 w 0 0 w)
  441.      (w 0 0 0 w w)
  442.      (w w w w w w))
  443.      (1 1) north)
  444.    )
  445.    (
  446.    (((w w w w w w)
  447.      (w 0 w 0 w w)
  448.      (w 0 w 2 0 w)
  449.      (w 1 3 0 w w)
  450.      (w w w w w w))
  451.      (3 3) north)
  452.  
  453.    (((w w w w w w)
  454.      (w 0 w 0 w w)
  455.      (w 0 w 0 0 w)
  456.      (w 0 0 0 w w)
  457.      (w w w w w w))
  458.      (1 1) north)
  459.   ))
  460.  )
Advertisement
Add Comment
Please, Sign In to add comment