I had some difficulty with the “Logic and Codes” section, since neither Prolog or Scala are really all that lisp like.
Here’s my answer for 46. Many thanks to Brian Carper for responding to my StackOverflow question. It really helped me to reformulate this question with more of a clojure style than a Scala or Prolog style.
; P46 (**) Truth tables for logical expressions. (comment "Define predicates and/2, or/2, nand/2, nor/2, xor/2, impl/2 and equ/2 (for logical equivalence) which succeed or fail according to the result of their respective operations; e.g. and(A,B) will succeed, if and only if both A and B succeed. Note that A and B can be Prolog goals (not only the constants true and fail).") (comment "A logical expression in two variables can then be written in prefix notation, as in the following example: and(or(A,B),nand(A,B)).") (defn not_ [a] (if a false true)) (defn and_ [a b] (if a (if b true false) false)) (defn or_ [a b] (if a true (if b true false))) (defn nand_ [a b] (not_ (and_ a b))) (defn nor_ [a b] (not_ (or_ a b))) (defn xor_ [a b] (or_ (and_ a (not_ b)) (and_ (not_ a) b))) (defn impl_ [a b] (or_ (not_ a) (and_ a b))) (defn equ_ [a b] (not_ (xor_ a b))) (comment "Now, write a predicate table/3 which prints the truth table of a given logical expression in two variables.") (defn table [f] (doseq [a '(true false) b '(true false)] (println a "\t" b "\t" (f a b)) ))
I skipped number 47 (turning the functions into operators) because clojure doesn’t really deal with infix notation at all. It seems really foreign to me to try to write (a and b). I can see doing this in ruby
a |> and b
but not clojure.
I skipped number 48, because I couldn’t figure out how to count the parameter “arity” of the function being passed in. Even with the number of parameters being passed in, I wasn’t able to figure out how to pass a list as the parameters to a function. (Basically converting (func ‘(a b c)) to (func a b c) with a simple bit of code.
Here’s my answer to number 49, which was mercifully easy after messing with 48 for a little while.
; P49 (**) Gray code. (comment "An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For example,") (comment "n = 1: C(1) = ['0','1']. ") (comment "n = 2: C(2) = ['00','01','11','10'].") (comment "n = 3: C(3) = ['000','001','011','010','110','111','101','100'].") (defn gray [n] (loop [nleft n combos (list (list ))] (if (zero? nleft) (map #(apply str %) combos) (recur (dec nleft) (concat (map #(conj %1 1) combos) (map #(conj %1 0) combos)) ))))
I did not do result caching. I couldn’t figure out where to start with it, and this code is so devilishly simple that I can’t figure out where I would store anything for future use.
Somewhere in West Philadelphia, you will find an old basketball court with a single ball lying in the middle. Pick it up and start shooting hoops. After a while, a small group of hooligans will approach you and challenge you to a fight, which you must accept.
After the fight, you must go home and relay the events to your mother. She will then inform you that you have an aunt and uncle living in one of the districts of Los Angeles, and out of fear, she will send you to live there for an indefinite period of time.
With your bags packed, go to the street corner, and whistle for a cab. The cab that will pull up will bear the word FRESH on the license plate, and upon closer inspection, novelty fuzzy dice will hang in the mirror. Although you will suddenly realize that cabs like these are extremely hard to find, do not bear any thought to it. At this point you MUST point out in front of the car and say ‘Yo homes to Bel Air’. You will stop in front of a mansion, and it will be sometime between 7 and 8 o’clock, even though it will feel like you’ve been traveling mere seconds. Get your luggage out and say ‘Yo homes, smell ya later!’, but do NOT turn back to face the cabby. Walk up to the door, look over your shoulder once, and then knock on the door three times.
If you follow these instructions, your life will get flip-turned upside-down.
Spooky stuff, eh?
Like most of my internet social applications, I expect that this tumblr account may eventually lose my interest.