Re:A week with Hy
by greenshackle2 on Thursday August 08, 2019 @03:56PM
(#20648034)
I played with it a bit, it's homoiconic, has macros, and papers over the difference between Python statements and expressions (so that everything is expressions).
Seems pretty neat, if you ask me.
Does Hy add its own "symbol" datatype, or use something in the standard library?
Re:A week with Hy
by greenshackle2 on Thursday August 08, 2019 @06:10PM
(#20649370)
Own symbol type
=> 'foo
HySymbol(u'foo')
Assignment and conditionals are expressions, however assignment returns None. => (setv bar (setv foo (if True True False)))
=> (type bar)
<type 'NoneType'>
Quoted expressions evaluate to Hy AST. => '(+ 1 2 3)
HyExpression([
HySymbol(u'+'),
HyInteger(1L),
HyInteger(2L),
HyInteger(3L)]
)
So it's kind of a frontend for the Python compiler, it goes:Hy AST -> Python AST -> Python bytecode
Users of Hy who don't know Lisp deserve to be informed that in an actual Lisp, it goes more like this:
=> 'foo
foo
(or, at worst FOO rather than foo). => '(+ 1 2 3)
(1 2 3)
Re:A week with Hy
by testaccount10 on Thursday August 08, 2019 @03:45PM
(#20647919)
Your example looks great! Now i want
(defmacro print-loop (line-start line-inc strings)
(let ((i line-start))
`(main
,(mapcar (lambda (x) `(linenumber ,(setq line-start (+ line-start line-inc)) (print ,x))) strings)
(goto line-start))))That's sort of the point. It's "just Python". It's s-expression syntax for Python.
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
- It effectively removes all restrictions based around things fitting on a single line - lambdas are the canonical example.Or in actual Lisp:
(tagbody
10 (print "hello world")
20 (print "Hey!")
(go 10))In many cases Python generators are better than lists: https://wiki.python.org/moin/Generators
E.g. consider xrange() vs (Python 2) range().
Re:A week with Hy
by yeezyseezy on Thursday August 08, 2019 @03:42PM
(#20647893)
The only real advantage I saw in the article is the addition of macros
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?