CH

February 27, 2014

In Which I Speak In Public on a Python Lisp

Filed under: Uncategorized — @ 12:00 a.m.
In Which I Speak In Public on a Python Lisp

"Whoever gave programmers the idea they should be talking in front of other people should be taken out back and shot."

Talk outline:

introduction

me

benkay @ <irc>

backends, infrastructure

bitcoin

lisp

business ∩ engineering ∩ design

hy

NOT PRODUCTION READY

get you a pylisp
$ brew install python3
$ virtualenv -p python3 hyplay
$ cd hyplay
$ source bin/activate
$ git clone http://github.com/hylang/hy
$ cd hy
$ python setup.py install
$ hy
=> (+ 1 2 3)
6

basics

addition:

=> (+ 1 2 3)
6

subtraction:

=> (- 3 2 1)
0

function nesting

=> (- (+ 1 2) (+ 2 1))
0

functions

ex1:

=> (defn add-two [added] (+ 2 added))
=> (add-two 4)
6

ex2:

=> (defn square [number]
     (** number 2))
=> (square 2)
4

NB: This was incorrect in the actual presentation.

ex3:

=> (defn square-root [num]
     (** num 0.5))
=> (square-root 25)
5.0
=> (square-root 27)
5.196152422706632

function composition

=> (defn add-four [added]
     (+ 2
       (add-two added)))
=> (add-four 4)
8

native python interop

lists

max

=> (max (list (range 10)))
9

min

=> (min (list (range 10)))
0

types

=> (type (square-root 27))
<class 'float'>

more types?

=> (type (range 20))
<class 'range'>

fun & crazy lispstuff

recursion

fibonacci number

mathy:
n_i = n_(i - 1) + n_(i - 2)
programmery:

current = previous + the-one-before-that

lispy:
=> (require hy.contrib.loop)
=> (defn fib-num [n]
    (loop [[i n]
	   [prev 0]
	   [cur 1]]
      (if (zero? i)
	cur
	(recur (dec i)
	       cur
	       (+ cur prev)))))
=> (list (take 10 (fib)))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
=> (nth (fib) 20)
6765

An IRC acquaintance (heyo jnerula!) wanted to know why this didn't use standard multiple-arity lisp stuff (his example being an Erlang method for multiple arity implementations. The answers are that a) I'm lazy (a positive quality in engineers) and b) that the point of this was to demonstrate recursion without hurting anyone's brains.

mapping

english

ap-map takes a function of the special variable 'it', and a list, returning a generator

example 1a

=> (require hy.contrib.anaphoric)
=> (ap-map (+ 1 it) [1 2 3 4])
<generator object _hy_anon_fn_1 at 0x10e71b320>

example 1b

=> (ap-map (+ 1 it) [1 2 3 4])
<generator object _hy_anon_fn_1 at 0x10e71b320>
=> (list (ap-map (+ 1 it) [1 2 3 4]))
[2, 3, 4, 5]

example 2

=> (ap-map (fib-num it) [1 2 3])
<generator object _hy_anon_fn_1 at 0x10f9acaa0>
=> (list (ap-map (fib-num it) [1 2 3 4 5]))
[1, 2, 3, 5, 8]
=> (list (ap-map (fib-num it) [0 1 100 22 99 54]))
[1, 1, 573147844013817084101, 28657, 354224848179261915075, 139583862445]

example 2b

=> (list (ap-map (fib-num it) (range 20)))
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]

function reuse!

fibonacci sequence

code
=> (require hy.contrib.anaphoric)
=> (defn fib-seq [n]
(list (ap-map (fib-num it) (range n))))
results
=> (fib-seq 2)
[1, 1]
=> (fib-seq 4)
[1, 1, 2, 3]
=> (fib-seq 10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
=> (fib-seq 100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]
=> (fib-seq 1000)
;; try it on your own box ;)

infinite sequences

fibonacci again…

code:
=> (defn fib []
     (setv a 0)
     (setv b 1)
     (while true
       (yield a)
       (setv (, a b) (, b (+ a b)))))
results
=> (list (take 10 (fib)))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

macros

macros re-write your code at run-time

example:
=> (defreader t [expr] `(, ~@expr))
=> #t(1 2 3 4)
(1, 2, 3, 4)

why lisp?

Everything is a list

Queues

Iteration

MapReduce

Wikipedia:

"A MapReduce program is composed of a Map() procedure that performs filtering and sorting (such as sorting students by first name into queues, one queue for each name) and a Reduce() procedure that performs a summary operation (such as counting the number of students in each queue, yielding name frequencies)"

Reduction in mental overhead

Extreme flexibility in shaping the language to the problem domain

Abstraction composability without excessive mental overhead

Selectively absorbs good ideas from the programming mainstream

why not lisp?

you may develop job dissatisfaction

perennial toolchain problem

emacs

vim

IDEs

light table

it can be darn hard to find lisp work

you will become a grouchy old person with bizarre notions about technology

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Reply

« Best ecommerce landing page of 2017 --- The CIA's flickr account »