(linenumber 10 (print "hello world"))
(linenumber 20 (print "Hey!"))
Coding in Lisp has nothing to do with putting parentheses everywhere but rather with thinking in some specific way. For instance, I know that I am ready to code something in Lisp when I see my whole task as a sequence of CONS, but how could Hy truly implement such a thing as long as it relies on Python lists?
Seems pretty neat, if you ask me.
=> (setv bar (setv foo (if True True False)))
=> (type bar)
=> '(+ 1 2 3)
Hy AST -> Python AST -> Python bytecode
=> '(+ 1 2 3)
(1 2 3)
(defmacro print-loop (line-start line-inc strings)
(let ((i line-start))
,(mapcar (lambda (x) `(linenumber ,(setq line-start (+ line-start line-inc)) (print ,x))) strings)
10 (print "hello world")
20 (print "Hey!")
Why would one want such a thing? Well, s-expressions confer many advantages (and of course, some disadvantages - it's good to have a choice!)
- It makes it easy to write code that writes code, which is useful because it affords "compile-time" abstractions with no runtime performance penalty (in lisp terms, "macros").
- If you're comfortable with a structure editor, it enormously lowers the friction involved in refactoring.
- It allows everything to be an expression:
json.loads(with f as open("file.json"): f.read()) #doesn't work
(json.loads (with [f (open "file.json")] (f.read))) ;works
E.g. consider xrange() vs (Python 2) range().