Fix typos and improve code examples

This commit is contained in:
Philip Xu 2018-05-16 02:36:15 -04:00
parent 643196c2b8
commit 0b08916174
5 changed files with 14 additions and 13 deletions

View File

@ -55,7 +55,7 @@ This results in the sequence ``[0 1 1 2 3 5 8 13 21 34 ...]``.
seq seq
=== ===
Usage: ``(seq [n] (* n n)`` Usage: ``(seq [n] (* n n))``
Creates a sequence defined in terms of ``n``. Creates a sequence defined in terms of ``n``.

View File

@ -233,7 +233,7 @@ as the user enters *k*.
.. code-block:: clj .. code-block:: clj
(while True (if (= "k" (raw-input "? ")) (while True (if (= "k" (input "? "))
(break) (break)
(print "Try again"))) (print "Try again")))
@ -1398,7 +1398,7 @@ repexpr]])``, but a less error-prone approach is to change the definition of
(defmacro foo [n] (defmacro foo [n]
`(do `(do
(require mymodule) (require mymodule)
(mymodule.repexpr ~n (raw-input "Gimme some input: ")))) (mymodule.repexpr ~n (input "Gimme some input: "))))
It's wise to use ``(require mymodule)`` here rather than ``(require [mymodule It's wise to use ``(require mymodule)`` here rather than ``(require [mymodule
[repexpr]])`` to avoid accidentally shadowing a function named ``repexpr`` in [repexpr]])`` to avoid accidentally shadowing a function named ``repexpr`` in

View File

@ -652,7 +652,7 @@ calling ``(f val-in-result val-in-latter)``.
.. code-block:: hy .. code-block:: hy
=> (merge-with (fn [x y] (+ x y)) {"a" 10 "b" 20} {"a" 1 "c" 30}) => (merge-with + {"a" 10 "b" 20} {"a" 1 "c" 30})
{u'a': 11L, u'c': 30L, u'b': 20L} {u'a': 11L, u'c': 30L, u'b': 20L}
@ -1201,9 +1201,9 @@ if *from-file* ends before a complete expression can be parsed.
4 4
=> (import io) => (import io)
=> (setv buffer (io.StringIO "(+ 2 2)\n(- 2 1)")) => (setv buffer (io.StringIO "(+ 2 2)\n(- 2 1)"))
=> (eval (read :from_file buffer)) => (eval (read :from-file buffer))
4 4
=> (eval (read :from_file buffer)) => (eval (read :from-file buffer))
1 1
=> (with [f (open "example.hy" "w")] => (with [f (open "example.hy" "w")]

View File

@ -323,13 +323,13 @@ Will turn into::
else: else:
_temp_name_here = False _temp_name_here = False
print _temp_name_here print(_temp_name_here)
OK, that was a bit of a lie, since we actually turn that statement OK, that was a bit of a lie, since we actually turn that statement
into:: into::
print True if True else False print(True if True else False)
By forcing things into an ``ast.expr`` if we can, but the general idea holds. By forcing things into an ``ast.expr`` if we can, but the general idea holds.
@ -412,10 +412,11 @@ so our re-written ``nif`` would look like:
(defmacro nif [expr pos-form zero-form neg-form] (defmacro nif [expr pos-form zero-form neg-form]
(with-gensyms [g] (with-gensyms [g]
`(setv [~g ~expr]) `(do
`(cond [(pos? ~g) ~pos-form] (setv ~g ~expr)
[(zero? ~g) ~zero-form] (cond [(pos? ~g) ~pos-form]
[(neg? ~g) ~neg-form]))) [(zero? ~g) ~zero-form]
[(neg? ~g) ~neg-form]))))
Finally, though we can make a new macro that does all this for us. :ref:`defmacro/g!` Finally, though we can make a new macro that does all this for us. :ref:`defmacro/g!`
will take all symbols that begin with ``g!`` and automatically call ``gensym`` with the will take all symbols that begin with ``g!`` and automatically call ``gensym`` with the

View File

@ -120,7 +120,7 @@ This is the basic premise of Lisp. Lisp stands for "list
processing"; this means that the structure of the program is processing"; this means that the structure of the program is
actually lists of lists. (If you're familiar with Python lists, actually lists of lists. (If you're familiar with Python lists,
imagine the entire same structure as above but with square brackets imagine the entire same structure as above but with square brackets
instead, any you'll be able to see the structure above as both a instead, and you'll be able to see the structure above as both a
program and a data structure.) This is easier to understand with more program and a data structure.) This is easier to understand with more
examples, so let's write a simple Python program, test it, and then examples, so let's write a simple Python program, test it, and then
show the equivalent Hy program:: show the equivalent Hy program::