Merge pull request #48 from willkg/docs-fixes
Syntax highlight blocks as cl
This commit is contained in:
commit
ccb75e62aa
@ -22,7 +22,9 @@ Basic intro to lisp for pythonistas
|
||||
|
||||
Okay, maybe you've never used lisp before, but you've used python!
|
||||
|
||||
A "hello world" in hy is actually super simple. Let's try it::
|
||||
A "hello world" in hy is actually super simple. Let's try it:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(print "hello world")
|
||||
|
||||
@ -31,18 +33,24 @@ version of::
|
||||
|
||||
print "hello world"
|
||||
|
||||
To add up some super simple math, we could do::
|
||||
To add up some super simple math, we could do:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(+ 1 3)
|
||||
|
||||
Which would return 4 and would be the equivalent of::
|
||||
Which would return 4 and would be the equivalent of:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
1 + 3
|
||||
|
||||
What you'll notice is that the first item in the list is the function
|
||||
being called and the rest of the arguments are the arguments being
|
||||
passed in. In fact, in hy (as with most lisps) we can pass in
|
||||
multiple arguments to the plus operator::
|
||||
multiple arguments to the plus operator:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(+ 1 3 55)
|
||||
|
||||
@ -54,7 +62,9 @@ is a great way to start learning lisp. The main thing that's obvious
|
||||
about lisp is that there's a lot of parentheses. This might seem
|
||||
confusing at first, but it isn't so hard. Let's look at some simple
|
||||
math that's wrapped in a bunch of parentheses that we could enter into
|
||||
the hy interpreter::
|
||||
the hy interpreter:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(def result (- (/ (+ 1 3 88) 2) 8))
|
||||
|
||||
@ -76,7 +86,9 @@ exercise first in python::
|
||||
# simplified to...
|
||||
result = 38
|
||||
|
||||
Now let's try the same thing in hy::
|
||||
Now let's try the same thing in hy:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(def result (- (/ (+ 1 3 88) 2) 8))
|
||||
; simplified to...
|
||||
@ -115,7 +127,9 @@ If we ran this program, it might go like::
|
||||
What is your age? 38
|
||||
Hello Gary! I see you are 38 years old.
|
||||
|
||||
Now let's look at the equivalent hy program::
|
||||
Now let's look at the equivalent hy program:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(defn simple-conversation []
|
||||
(print "Hello! I'd like to get to know you. Tell me about yourself!")
|
||||
@ -171,7 +185,9 @@ hy. Let's experiment with this in the hy interpreter::
|
||||
{'dog': 'bark', 'cat': 'meow'}
|
||||
|
||||
(You may notice that at present, the common lisp method of quoting
|
||||
things like so::
|
||||
things like so:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
'(1 2 3)
|
||||
|
||||
@ -187,12 +203,16 @@ What's this? Yes indeed, this is precisely the same as::
|
||||
" fooooo ".strip()
|
||||
|
||||
That's right... lisp with dot notation! If we have this string
|
||||
assigned as a variable, we can also do the following::
|
||||
assigned as a variable, we can also do the following:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(def this-string " fooooo ")
|
||||
(this-string.strip)
|
||||
|
||||
What about conditionals?::
|
||||
What about conditionals?:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(if (try-some-thing)
|
||||
(print "this is if true")
|
||||
@ -214,7 +234,9 @@ called "cond". In python, you might do something like::
|
||||
else:
|
||||
print "That variable is jussssst right!"
|
||||
|
||||
In hy, you would do::
|
||||
In hy, you would do:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(cond
|
||||
((> somevar 50)
|
||||
@ -231,7 +253,9 @@ notice that the "else" is implemented at the end simply by checking
|
||||
for "true"... that's because true will always be true, so if we get
|
||||
this far, we'll always run that one!
|
||||
|
||||
You might notice above that if you have code like::
|
||||
You might notice above that if you have code like:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(if some-condition
|
||||
(body-if-true)
|
||||
@ -240,7 +264,9 @@ You might notice above that if you have code like::
|
||||
But wait! What if you want to execute more than one statment in the
|
||||
body of one of these?
|
||||
|
||||
You can do the following::
|
||||
You can do the following:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(if (try-some-thing)
|
||||
(do
|
||||
@ -273,7 +299,9 @@ TODO: explain the extra power of hy's for, the list comprehensions
|
||||
aspect ;)
|
||||
|
||||
You can also import and make use of various python libraries. For
|
||||
example::
|
||||
example:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(import os)
|
||||
|
||||
@ -281,6 +309,13 @@ example::
|
||||
(os.mkdir "/tmp/somedir/anotherdir")
|
||||
(print "Hey, that path isn't there!"))
|
||||
|
||||
Comments start with semicolons:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(print "this will run")
|
||||
; (print "but this will not")
|
||||
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
||||
|
||||
|
||||
TODO
|
||||
|
@ -2,5 +2,5 @@ tox
|
||||
nose
|
||||
astor
|
||||
flake8
|
||||
sphynx
|
||||
Sphinx
|
||||
coverage
|
||||
|
Loading…
x
Reference in New Issue
Block a user