Created
          January 16, 2014 20:22 
        
      - 
      
 - 
        
Save bendisposto/8462720 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | (ns muster.core | |
| (:use clojure.set)) | |
| ;; Aufgabe 1 | |
| (defn calc' [a op b & rest] | |
| (if-not (seq rest) | |
| (op a b) | |
| (apply calc' (op a b) rest))) | |
| (defmacro calc [bindings & term] | |
| `(let ~bindings (calc' ~@term))) | |
| ;; Aufgabe 2 | |
| (defmacro dfa-state [action m] | |
| `(fn [w#] (if (seq w#) | |
| (let [[h# & t#] w# | |
| a# (get ~m h#)] | |
| (if (nil? a#) :reject #(a# t#))) | |
| ~action))) | |
| (defn dfa-match [init text] (trampoline init text)) | |
| ;; Aufgabe 3 | |
| (def graph | |
| {:nodes | |
| #{:tshirt :hose :pullover | |
| :jacke :schuhe :socken | |
| :guertel :unterhose} | |
| :transitions [ | |
| [:tshirt :pullover] | |
| [:pullover :jacke] | |
| [:unterhose :hose] | |
| [:hose :guertel] | |
| [:unterhose :guertel] | |
| [:hose :schuhe] | |
| [:socken :schuhe]]} | |
| ) | |
| (defn forbidden [transitions] | |
| (set (map first (group-by second transitions)))) | |
| (defn allowed [{:keys [nodes transitions]}] | |
| (difference | |
| nodes | |
| (forbidden transitions))) | |
| (defn init [graph] [{:path [] :graph graph}]) | |
| (defn step [choice {:keys [path graph]}] | |
| {:path (conj path choice) :graph {:nodes (disj (:nodes graph) choice) | |
| :transitions | |
| (remove (fn [[t _]] (= t choice)) | |
| (:transitions graph))}}) | |
| (defn help-j-step [gs] | |
| (mapcat (fn [g] (for [c (allowed (:graph g))] (step c g))) gs)) | |
| (defn help-j [g] | |
| (map :path (let [gi (init g) | |
| c (inc (count (:nodes g)))] | |
| (last (take c (iterate help-j-step gi)))))) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment