Change the examples in the tutorial to Python 3
This commit is contained in:
parent
63958403b4
commit
9afb196c8b
@ -26,6 +26,9 @@ This is pretty cool because it means Hy is several things:
|
|||||||
comfort of Python!
|
comfort of Python!
|
||||||
- For everyone: a pleasant language that has a lot of neat ideas!
|
- 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
|
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
|
See? Easy! As you may have guessed, this is the same as the Python
|
||||||
version of::
|
version of::
|
||||||
|
|
||||||
print "hello world"
|
print("hello world")
|
||||||
|
|
||||||
To add up some super simple math, we could do:
|
To add up some super simple math, we could do:
|
||||||
|
|
||||||
@ -78,7 +81,7 @@ the Hy interpreter:
|
|||||||
|
|
||||||
(setv result (- (/ (+ 1 3 88) 2) 8))
|
(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::
|
expression in python::
|
||||||
|
|
||||||
result = ((1 + 3 + 88) / 2) - 8
|
result = ((1 + 3 + 88) / 2) - 8
|
||||||
@ -92,9 +95,9 @@ exercise first in Python::
|
|||||||
# simplified to...
|
# simplified to...
|
||||||
result = (92 / 2) - 8
|
result = (92 / 2) - 8
|
||||||
# simplified to...
|
# simplified to...
|
||||||
result = 46 - 8
|
result = 46.0 - 8
|
||||||
# simplified to...
|
# simplified to...
|
||||||
result = 38
|
result = 38.0
|
||||||
|
|
||||||
Now let's try the same thing in Hy:
|
Now let's try the same thing in Hy:
|
||||||
|
|
||||||
@ -104,12 +107,12 @@ Now let's try the same thing in Hy:
|
|||||||
; simplified to...
|
; simplified to...
|
||||||
(setv result (- (/ 92 2) 8))
|
(setv result (- (/ 92 2) 8))
|
||||||
; simplified to...
|
; simplified to...
|
||||||
(setv result (- 46 8))
|
(setv result (- 46.0 8))
|
||||||
; simplified to...
|
; simplified to...
|
||||||
(setv result 38)
|
(setv result 38.0)
|
||||||
|
|
||||||
As you probably guessed, this last expression with ``setv`` means to
|
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!
|
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::
|
show the equivalent Hy program::
|
||||||
|
|
||||||
def simple_conversation():
|
def simple_conversation():
|
||||||
print "Hello! I'd like to get to know you. Tell me about yourself!"
|
print("Hello! I'd like to get to know you. Tell me about yourself!")
|
||||||
name = raw_input("What is your name? ")
|
name = input("What is your name? ")
|
||||||
age = raw_input("What is your age? ")
|
age = input("What is your age? ")
|
||||||
print "Hello " + name + "! I see you are " + age + " years old."
|
print("Hello " + name + "! I see you are " + age + " years old.")
|
||||||
|
|
||||||
simple_conversation()
|
simple_conversation()
|
||||||
|
|
||||||
@ -143,8 +146,8 @@ Now let's look at the equivalent Hy program:
|
|||||||
|
|
||||||
(defn simple-conversation []
|
(defn simple-conversation []
|
||||||
(print "Hello! I'd like to get to know you. Tell me about yourself!")
|
(print "Hello! I'd like to get to know you. Tell me about yourself!")
|
||||||
(setv name (raw-input "What is your name? "))
|
(setv name (input "What is your name? "))
|
||||||
(setv age (raw-input "What is your age? "))
|
(setv age (input "What is your age? "))
|
||||||
(print (+ "Hello " name "! I see you are "
|
(print (+ "Hello " name "! I see you are "
|
||||||
age " years old.")))
|
age " years old.")))
|
||||||
|
|
||||||
@ -223,7 +226,7 @@ supports the Common Lisp method of quoting:
|
|||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
=> '(1 2 3)
|
=> '(1 2 3)
|
||||||
(1L 2L 3L)
|
(1 2 3)
|
||||||
|
|
||||||
You also have access to all the built-in types' nice methods::
|
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
|
somevar = 33
|
||||||
if somevar > 50:
|
if somevar > 50:
|
||||||
print "That variable is too big!"
|
print("That variable is too big!")
|
||||||
elif somevar < 10:
|
elif somevar < 10:
|
||||||
print "That variable is too small!"
|
print("That variable is too small!")
|
||||||
else:
|
else:
|
||||||
print "That variable is jussssst right!"
|
print("That variable is jussssst right!")
|
||||||
|
|
||||||
In Hy, you would do:
|
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::
|
we might do::
|
||||||
|
|
||||||
for i in range(10):
|
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:
|
The equivalent in Hy would be:
|
||||||
|
|
||||||
@ -361,7 +364,7 @@ Python's context managers (``with`` statements) are used like this:
|
|||||||
which is equivalent to::
|
which is equivalent to::
|
||||||
|
|
||||||
with open("/tmp/data.in") as f:
|
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::
|
And yes, we do have List comprehensions! In Python you might do::
|
||||||
|
|
||||||
@ -484,7 +487,7 @@ like::
|
|||||||
And we might use it like::
|
And we might use it like::
|
||||||
|
|
||||||
bar = FooBar(1)
|
bar = FooBar(1)
|
||||||
print bar.get_x()
|
print(bar.get_x())
|
||||||
|
|
||||||
|
|
||||||
In Hy:
|
In Hy:
|
||||||
@ -659,12 +662,16 @@ Let's take the classic:
|
|||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
|
(require [hy.contrib.loop [loop]])
|
||||||
|
|
||||||
(loop (print (eval (read))))
|
(loop (print (eval (read))))
|
||||||
|
|
||||||
Rather than write it like that, we can write it as follows:
|
Rather than write it like that, we can write it as follows:
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
|
(require [hy.contrib.loop [loop]])
|
||||||
|
|
||||||
(-> (read) (eval) (print) (loop))
|
(-> (read) (eval) (print) (loop))
|
||||||
|
|
||||||
Now, using `python-sh <http://amoffat.github.com/sh/>`_, we can show
|
Now, using `python-sh <http://amoffat.github.com/sh/>`_, we can show
|
||||||
|
Loading…
x
Reference in New Issue
Block a user