Merge pull request #1386 from freezas/master

Tutorial in Python3
This commit is contained in:
Kodi Arfer 2017-08-27 14:25:59 -07:00
commit 3db13ec71f
2 changed files with 28 additions and 20 deletions

View File

@ -82,3 +82,4 @@
* David Schaefer <david.schaefe@gmail.com>
* Jordan Danford <jordandanford@gmail.com>
* Andrew Silva <asilva@law.harvard.edu>
* Zaheer Soebhan <z.soebhan@gmail.com>

View File

@ -26,6 +26,9 @@ This is pretty cool because it means Hy is several things:
comfort of Python!
- For everyone: a pleasant language that has a lot of neat ideas!
Now this tutorial assumes you're running Hy on Python 3. So know things
are a bit different if you're still using Python 2.
Basic intro to Lisp for Pythonistas
===================================
@ -41,7 +44,7 @@ A "hello world" program in Hy is actually super simple. Let's try it:
See? Easy! As you may have guessed, this is the same as the Python
version of::
print "hello world"
print("hello world")
To add up some super simple math, we could do:
@ -78,7 +81,7 @@ the Hy interpreter:
(setv result (- (/ (+ 1 3 88) 2) 8))
This would return 38. But why? Well, we could look at the equivalent
This would return 38.0 But why? Well, we could look at the equivalent
expression in python::
result = ((1 + 3 + 88) / 2) - 8
@ -92,9 +95,9 @@ exercise first in Python::
# simplified to...
result = (92 / 2) - 8
# simplified to...
result = 46 - 8
result = 46.0 - 8
# simplified to...
result = 38
result = 38.0
Now let's try the same thing in Hy:
@ -104,12 +107,12 @@ Now let's try the same thing in Hy:
; simplified to...
(setv result (- (/ 92 2) 8))
; simplified to...
(setv result (- 46 8))
(setv result (- 46.0 8))
; simplified to...
(setv result 38)
(setv result 38.0)
As you probably guessed, this last expression with ``setv`` means to
assign the variable "result" to 38.
assign the variable "result" to 38.0.
See? Not too hard!
@ -123,10 +126,10 @@ examples, so let's write a simple Python program, test it, and then
show the equivalent Hy program::
def simple_conversation():
print "Hello! I'd like to get to know you. Tell me about yourself!"
name = raw_input("What is your name? ")
age = raw_input("What is your age? ")
print "Hello " + name + "! I see you are " + age + " years old."
print("Hello! I'd like to get to know you. Tell me about yourself!")
name = input("What is your name? ")
age = input("What is your age? ")
print("Hello " + name + "! I see you are " + age + " years old.")
simple_conversation()
@ -143,8 +146,8 @@ Now let's look at the equivalent Hy program:
(defn simple-conversation []
(print "Hello! I'd like to get to know you. Tell me about yourself!")
(setv name (raw-input "What is your name? "))
(setv age (raw-input "What is your age? "))
(setv name (input "What is your name? "))
(setv age (input "What is your age? "))
(print (+ "Hello " name "! I see you are "
age " years old.")))
@ -223,7 +226,7 @@ supports the Common Lisp method of quoting:
.. code-block:: clj
=> '(1 2 3)
(1L 2L 3L)
(1 2 3)
You also have access to all the built-in types' nice methods::
@ -260,11 +263,11 @@ called ``cond``. In Python, you might do something like::
somevar = 33
if somevar > 50:
print "That variable is too big!"
print("That variable is too big!")
elif somevar < 10:
print "That variable is too small!"
print("That variable is too small!")
else:
print "That variable is jussssst right!"
print("That variable is jussssst right!")
In Hy, you would do:
@ -330,7 +333,7 @@ Looping is not hard but has a kind of special structure. In Python,
we might do::
for i in range(10):
print "'i' is now at " + str(i)
print("'i' is now at " + str(i))
The equivalent in Hy would be:
@ -361,7 +364,7 @@ Python's context managers (``with`` statements) are used like this:
which is equivalent to::
with open("/tmp/data.in") as f:
print f.read()
print(f.read())
And yes, we do have List comprehensions! In Python you might do::
@ -484,7 +487,7 @@ like::
And we might use it like::
bar = FooBar(1)
print bar.get_x()
print(bar.get_x())
In Hy:
@ -659,12 +662,16 @@ Let's take the classic:
.. code-block:: clj
(require [hy.contrib.loop [loop]])
(loop (print (eval (read))))
Rather than write it like that, we can write it as follows:
.. code-block:: clj
(require [hy.contrib.loop [loop]])
(-> (read) (eval) (print) (loop))
Now, using `python-sh <http://amoffat.github.com/sh/>`_, we can show