summaryrefslogtreecommitdiffhomepage
path: root/hy.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'hy.html.markdown')
-rw-r--r--hy.html.markdown91
1 files changed, 37 insertions, 54 deletions
diff --git a/hy.html.markdown b/hy.html.markdown
index fc37d537..11412f09 100644
--- a/hy.html.markdown
+++ b/hy.html.markdown
@@ -6,59 +6,54 @@ contributors:
- ["Zirak", "http://zirak.me"]
---
-Hy is a lisp dialect built on top of python. This is achieved by
-converting hy code to python's abstract syntax tree (ast). This allows
-hy to call native python code or python to call native hy code as well
+Hy is a Lisp dialect built on top of Python. This is achieved by
+converting Hy code to Python's abstract syntax tree (AST). This allows
+Hy to call native Python code or Python to call native Hy code as well
-This tutorial works for hy ≥ 0.9.12, with some corrections for hy 0.11.
+```hylang
+; Semicolon comments, like other Lisps
-```clojure
-;; this gives an gentle introduction to hy
-;;
-; Semicolon comments, like other LISPS
-
-;; s-expression basics
-; lisp programs are made of symbolic expressions or sexps which
+;; S-expression basics
+; Lisp programs are made of symbolic expressions or sexps which
; resemble
(some-function args)
; now the quintessential hello world
(print "hello world")
-;; simple data types
-; All simple data types are exactly similar to their python counterparts
-; which
+;; Simple data types
+; All simple data types are the same as their Python counterparts
42 ; => 42
3.14 ; => 3.14
True ; => True
4+10j ; => (4+10j) a complex number
-; lets start with some really simple arithmetic
+; lets start with some simple arithmetic
(+ 4 1) ;=> 5
-; the operator is applied to all arguments, like other lisps
+; the operator is applied to all arguments, like other Lisps
(+ 4 1 2 3) ;=> 10
(- 2 1) ;=> 1
(* 4 2) ;=> 8
(/ 4 1) ;=> 4
(% 4 2) ;=> 0 the modulo operator
-; power is represented by ** operator like python
+; power is represented by the ** operator, like Python
(** 3 2) ;=> 9
; nesting forms will do the expected thing
(+ 2 (* 4 2)) ;=> 10
-; also logical operators and or not and equal to etc. do as expected
+; also logical operators and or not and equal to etc. work as expected
(= 5 4) ;=> False
(not (= 5 4)) ;=> True
-;; variables
+;; Variables
; variables are set using setv, variable names can use utf-8 except
; for ()[]{}",'`;#|
(setv a 42)
(setv π 3.14159)
(def *foo* 42)
-;; other container data types
+;; Other container data types
; strings, lists, tuples & dicts
-; these are exactly same as python's container types
+; these are exactly same as Python's container types
"hello world" ;=> "hello world"
-; string operations work similar to python
+; string operations work similar to Python
(+ "hello " "world") ;=> "hello world"
; lists are created using [], indexing starts at 0
(setv mylist [1 2 3 4])
@@ -66,15 +61,15 @@ True ; => True
(setv mytuple (, 1 2))
; dictionaries are key value pairs
(setv dict1 {"key1" 42 "key2" 21})
-; :name can be used to define keywords in hy which can be used for keys
+; :name can be used to define keywords in Hy which can be used for keys
(setv dict2 {:key1 41 :key2 20})
; use `get' to get the element at an index/key
(get mylist 1) ;=> 2
(get dict1 "key1") ;=> 42
-; Alternatively if keywords were used they can directly be called
+; Alternatively if keywords were used they can be called directly
(:key1 dict2) ;=> 41
-;; functions and other program constructs
+;; Functions and other program constructs
; functions are defined using defn, the last sexp is returned by default
(defn greet [name]
"A simple greeting" ; an optional docstring
@@ -96,7 +91,7 @@ True ; => True
(print "And it also has:" props))
(something-fancy "My horse" "amazing" :mane "spectacular")
-
+
; you use apply instead of the splat operators:
(apply something-fancy ["My horse" "amazing"] { "mane" "spectacular" })
@@ -105,21 +100,19 @@ True ; => True
(map (fn [x] (* x x)) [1 2 3 4]) ;=> [1 4 9 16]
;; Sequence operations
-; hy has some builtin utils for sequence operations etc.
+; Hy has some builtin utils for sequence operations etc.
; retrieve the first element using `first' or `car'
(setv mylist [1 2 3 4])
(setv mydict {"a" 1 "b" 2})
(first mylist) ;=> 1
-; slice lists using slice
-(slice mylist 1 3) ;=> [2 3]
-; or, in hy 0.11, use cut instead:
+; slice lists using cut
(cut mylist 1 3) ;=> [2 3]
; get elements from a list or dict using `get'
(get mylist 1) ;=> 2
(get mydict "b") ;=> 2
-; list indexing starts from 0 same as python
+; list indexing starts from 0, same as Python
; assoc can set elements at keys/indexes
(assoc mylist 2 10) ; makes mylist [1 2 10 4]
(assoc mydict "c" 3) ; makes mydict {"a" 1 "b" 2 "c" 3}
@@ -127,11 +120,11 @@ True ; => True
; sequences fun
;; Python interop
-;; import works just like in python
+;; import works just like in Python
(import datetime)
-(import [functools [partial reduce]]) ; imports fun1 and fun2 from module1
-(import [matplotlib.pyplot :as plt]) ; doing an import foo as bar
-; all builtin python methods etc. are accessible from hy
+(import functools [partial reduce]) ; imports partial and reduce from functools
+(import matplotlib.pyplot :as plt) ; imports foo as bar
+; all builtin Python methods etc. are accessible from Hy
; a.foo(arg) is called as (.foo a arg)
(.split (.strip "hello world ")) ;=> ["hello" "world"]
@@ -166,8 +159,8 @@ True ; => True
; group statements with do, these are executed sequentially
; forms like defn have an implicit do
(do
- (setv someval 10)
- (print "someval is set to " someval)) ;=> 10
+ (setv someval 10)
+ (print "someval is set to " someval)) ;=> 10
; create lexical bindings with `let', all variables defined thusly
; have local scope
@@ -175,34 +168,24 @@ True ; => True
"sherlock" "moriarty"
"seinfeld" "newman"}]
(for [[h v] (.items nemesis)]
- (print (.format "{0}'s nemesis was {1}" h v))))
+ (print (.format "{0}'s nemesis was {1}" h v))))
-;; classes
+;; Classes
; classes are defined in the following way
(defclass Wizard [object]
- [[--init-- (fn [self spell]
- (setv self.spell spell) ; init the spell attr
- None)]
- [get-spell (fn [self]
- self.spell)]])
-
-; or, in hy 0.11:
-(defclass Wizard [object]
- (defn --init-- [self spell]
+ (defn __init__ [self spell]
(setv self.spell spell))
(defn get-spell [self]
self.spell))
-
-;; do checkout hylang.org
```
### Further Reading
-This tutorial is just a very basic introduction to hy/lisp/python.
+This tutorial is just a basic introduction to Hy/Lisp/Python.
-Hy docs are here: [http://hy.readthedocs.org](http://hy.readthedocs.org)
+Hy docs are here: [https://hylang.org/hy/doc](https://hylang.org/hy/doc)
-Hy's GitHub repo: [http://github.com/hylang/hy](http://github.com/hylang/hy)
+Hy's GitHub repo: [https://github.com/hylang/hy](https://github.com/hylang/hy)
-On freenode irc #hy, twitter hashtag #hylang
+On freenode IRC `#hy`, twitter hashtag #hylang