A popular programming challenge is to, in your favorite programming language, write a Quine -- a program/function that prints its own source code. Usually, writing a Quine is an incredibly mindbending effort. However, yesterday, it was pointed out to me that in Clojure, this task is ridiculously simple:
(defn self-source
"prints the source of itself"
[]
(source self-source))
Where's the challenge in that? :-)
No it doesn't!
ReplyDeleteuser=>
(defn self-source
"prints the source of itself"
[]
(source self-source))
#'user/self-source
user=> (self-source)
Source not found
nil
user=>
Thoughts On Programming: Clojure Makes Quines Way Too Easy >>>>> Download Now
Delete>>>>> Download Full
Thoughts On Programming: Clojure Makes Quines Way Too Easy >>>>> Download LINK
>>>>> Download Now
Thoughts On Programming: Clojure Makes Quines Way Too Easy >>>>> Download Full
>>>>> Download LINK Xg
The source function doesn't work on functions defined interactively the REPL. Put it in a file, and send the contents of your file to the REPL, and self-source will work just fine. (I tested this in Eclipse with the Counterclockwise plug-in, on Clojure 1.4.0)
ReplyDeleteReason it works: When you send the entire contents of the file to the REPL, the metadata gets annotated with the line number where the function is defined. source uses that information to get at the source code.
This comment has been removed by the author.
ReplyDeleteUsing an API to access the source form a the function's code has never been a valid way to define a quine. Equally invalid are "quines" that make a call to copy the source file for the code (with or without using metadata for the path).
ReplyDeleteA quine in any LISP has always been fairly trivial. For Clojure just replace lambda with fn:
((fn [x] (list x (list (quote quote) x))) (quote (fn [x] (list x (list (quote quote) x)))))
You can make it a bit more idiomatically Clojure thusly:
(#(list % (list (quote quote) %)) '#(list % (list (quote quote) %)))
Which gives an expansion that is a true quine:
((fn* [p1__20#] (list p1__20# (list (quote quote) p1__20#))) (quote (fn* [p1__20#] (list p1__20# (list (quote quote) p1__20#)))))
Shorter yet:
ReplyDelete(#(list % (list 'quote %)) '#(list % (list 'quote %)))
Evaluates to the quine:
((fn* [p1__48#] (list p1__48# (list (quote quote) p1__48#)))
(quote (fn* [p1__48#] (list p1__48# (list (quote quote) p1__48#)))))
@28:10 "It's Quine Time"
ReplyDeletehttp://www.infoq.com/presentations/miniKanren
https://github.com/webyrd/quines
thank you for sharing post.
ReplyDeleteweb programming tutorial
welookups
ReplyDeletebuy-bensedin-online-diazepam-10mg
buy-actavis-promethazine-online
buy-valium-online
buy-methadone-online
We are an online and retail dealer and distribution center based in USA. We specialize in providing quality firearms and accessories to registered buyers
ReplyDeleteberetta px4 storm for sale
sig sauer p938 for sale
glock 30 for sale
beretta for sale
Thoughts On Programming: Clojure Makes Quines Way Too Easy >>>>> Download Now
ReplyDelete>>>>> Download Full
Thoughts On Programming: Clojure Makes Quines Way Too Easy >>>>> Download LINK
>>>>> Download Now
Thoughts On Programming: Clojure Makes Quines Way Too Easy >>>>> Download Full
>>>>> Download LINK wm