Skip to content

Instantly share code, notes, and snippets.

@robbielynch
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbielynch/10775415 to your computer and use it in GitHub Desktop.
Save robbielynch/10775415 to your computer and use it in GitHub Desktop.
IErlang Notebook - April 15, 2014
{
"metadata": {
"name": "",
"signature": "sha256:58714955d7aa791882dbb33fa58c40d18a9723a3a9f223aa113fd27fe4402a69"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"<img src=\"http://i.imgur.com/7rr6Tbr.png\" title=\"Hosted by imgur.com\"/>\n",
"\n",
"##April 20, 2014\n",
">Developed by [Robbie Lynch](http://roblynch.info)\n",
"\n",
">Supervised by [Paul Barry](http://glasnost.itcarlow.ie/~barryp/)\n",
"\n",
">Institute of Technology, Carlow, Ireland.\n",
"\n",
"----\n",
"\n",
"##Download\n",
"Download the IErlang Kernel for IPython at http://github.com/robbielynch/ierlang\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#Data Types"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AnInt = 1234."
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"html": [
"<pre>1234</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"1234"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AFloat = 55 / 3."
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"html": [
"<pre>18.333333333333332</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"18.333333333333332"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AString = \"Awesome!\"."
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"html": [
"<pre>\"Awesome!\"</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"\"Awesome!\""
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AnAtom = 'This is an atom with spaces'."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>'This is an atom with spaces'</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"'This is an atom with spaces'"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"atom_with_no_spaces."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>atom_with_no_spaces</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"atom_with_no_spaces"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"{this, is, a, tuple_of_atoms}."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>{this,is,a,tuple_of_atoms}</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"{this,is,a,tuple_of_atoms}"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AList = [1,2,3,4,5]."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>[1,2,3,4,5]</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"[1,2,3,4,5]"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Variable Bindings\n",
"i.e. Referring to previously assigned variables."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AVariable = 100."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>100</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"100"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AnotherVar = 77."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>77</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"77"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"AVariable + AnotherVar."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>177</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"177"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"NedStark = [[]|[AnotherVar + AVariable]]."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>[[],177]</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"[[],177]"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"NedStark."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>[[],177]</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"[[],177]"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Built in Functions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lists:sort([10,9,8,3,5,4,6,1,7,2])."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>[1,2,3,4,5,6,7,8,9,10]</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"[1,2,3,4,5,6,7,8,9,10]"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"io:format(\"hello\")."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>ok</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"ok"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"is_integer(4325345)."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>true</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"text": [
"true"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"os:cmd(\"pwd\")."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>\"/home/robbie/dev/ierlang/src<br />\"</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"\"/home/robbie/dev/ierlang/src\\n\""
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"is_binary(<<\"Hello\">>)."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>true</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": [
"true"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Expressions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"9283479238472398472398472394872398 * 28347632847623847623847623847623847623847632874623864."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>263164661000754185502995313263072243291280523500406936157710040110469708162166525705872</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
"263164661000754185502995313263072243291280523500406936157710040110469708162166525705872"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"99 div 5. %%Integer division"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>19</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"text": [
"19"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"99 / 5."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>19.8</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
"19.8"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"893475 - 98234798432579348753."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>-98234798432578455278</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"-98234798432578455278"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Errors"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"this will throw an error"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>{badmatch,{error,{1,erl_parse,[\"syntax error before: \",\"will\"]}}}</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"{badmatch,{error,{1,erl_parse,[\"syntax error before: \",\"will\"]}}}"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lists:flatten(notalist)."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>function_clause</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"function_clause"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"55 div 44.3."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>badarith</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
"badarith"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Terminal Commands\n",
"To make interacion with the terminal quicker and easier, you can run terminal commands\n",
"\n",
"by pre-pending `;;` before the command. For example:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
";;pwd"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>\"/home/robbie/dev/ierlang/src<br />\"</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
"\"/home/robbie/dev/ierlang/src\\n\""
]
}
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
";;date"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>\"Sun Apr 20 16:56:04 BST 2014<br />\"</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"text": [
"\"Sun Apr 20 16:56:04 BST 2014\\n\""
]
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
";;whereis ipython"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>\"ipython: /usr/bin/ipython2.7 /usr/bin/X11/ipython2.7 /usr/local/bin/ipython /usr/share/man/man1/ipython.1.gz<br />\"</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 27,
"text": [
"\"ipython: /usr/bin/ipython2.7 /usr/bin/X11/ipython2.7 /usr/local/bin/ipython /usr/share/man/man1/ipython.1.gz\\n\""
]
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
";;ping -c 5 google.com"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>\"PING google.com (74.125.24.101) 56(84) bytes of data.<br />64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=1 ttl=44 time=60.0 ms<br />64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=2 ttl=44 time=68.5 ms<br />64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=3 ttl=44 time=47.4 ms<br />64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=4 ttl=44 time=67.1 ms<br />64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=5 ttl=44 time=45.8 ms<br /><br />--- google.com ping statistics ---<br />5 packets transmitted, 5 received, 0% packet loss, time 4004ms<br />rtt min/avg/max/mdev = 45.816/57.810/68.506/9.563 ms<br />\"</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 28,
"text": [
"\"PING google.com (74.125.24.101) 56(84) bytes of data.\\n64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=1 ttl=44 time=60.0 ms\\n64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=2 ttl=44 time=68.5 ms\\n64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=3 ttl=44 time=47.4 ms\\n64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=4 ttl=44 time=67.1 ms\\n64 bytes from de-in-f101.1e100.net (74.125.24.101): icmp_req=5 ttl=44 time=45.8 ms\\n\\n--- google.com ping statistics ---\\n5 packets transmitted, 5 received, 0% packet loss, time 4004ms\\nrtt min/avg/max/mdev = 45.816/57.810/68.506/9.563 ms\\n\""
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
";;ls mochi*"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>\"mochijson2.beam<br />mochijson2.erl<br />mochinum.beam<br />mochinum.erl<br />\"</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 29,
"text": [
"\"mochijson2.beam\\nmochijson2.erl\\nmochinum.beam\\nmochinum.erl\\n\""
]
}
],
"prompt_number": 29
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##We can even compile erlang via the terminal commands"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
";;erlc mochijson2.erl"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>[]</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 30,
"text": [
"[]"
]
}
],
"prompt_number": 30
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
">It's possible to start the original IPython Notebook with the command \n",
"```\n",
";;ipython notebook\n",
"```\n",
"Although, it does kill our kernel :("
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Modules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###We can create, save and compile erlang modules\n",
">And if we deliberatly leave an error on line 4... it will tell us...."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-module(super_awesome_module). %%First line must begin with -module().\n",
"-export([my_favorite_numbers/0, unlucky_number/0]).\n",
"\n",
"my_favorite_numbers()- %%<---------------------This is where the error is%%\n",
"[1, 2, 3, 4, 5].\n",
"\n",
"unlucky_number()->\n",
"((278346287 * 98324928374) - (829437 * 3476) - (27368378729560724309 + (2 * 2)))."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>super_awesome_module.erl:4: syntax error before: '-'<br />super_awesome_module.erl:2: function my_favorite_numbers/0 undefined<br /></pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 31,
"text": [
"super_awesome_module.erl:4: syntax error before: '-'\n",
"super_awesome_module.erl:2: function my_favorite_numbers/0 undefined\n"
]
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
">It also tells us when things work..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-module(super_awesome_module). %%First line must begin with -module().\n",
"-export([my_favorite_numbers/0, unlucky_number/0]).\n",
"\n",
"my_favorite_numbers()->\n",
"[1, 2, 3, 4, 5].\n",
"\n",
"unlucky_number()->\n",
"((278346287 * 98324928374) - (829437 * 3476) - (27368378729560724309 + (2 * 2)))."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>Successfully Compiled</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 32,
"text": [
"Successfully Compiled"
]
}
],
"prompt_number": 32
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Now for the cool part\n",
">We can reference the exported functions from our compiled modules"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"super_awesome_module:my_favorite_numbers()."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>[1,2,3,4,5]</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 33,
"text": [
"[1,2,3,4,5]"
]
}
],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lists:reverse(super_awesome_module:my_favorite_numbers())."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>[5,4,3,2,1]</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 34,
"text": [
"[5,4,3,2,1]"
]
}
],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"super_awesome_module:unlucky_number()."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre>13</pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 35,
"text": [
"13"
]
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"MyPid = spawn(super_awesome_module, unlucky_number, [])."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre><0.49.0></pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 36,
"text": [
"<0.49.0>"
]
}
],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"MyPid."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<pre><0.49.0></pre>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 37,
"text": [
"<0.49.0>"
]
}
],
"prompt_number": 37
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Some Problems\n",
"####as of April 16, 2014"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Encoding tuples inside other data structures\n",
"* ~~Encoding floats inside other data structures~~\n",
"* ~~**ERROR MESSAGES** - They do not appear (Something to do with pyerr on ierlang side)~~\n",
"* Cannot create notebooks outside ierlang/src directory\n",
"* Horrible installation and setup\n",
"* Having to edit the IPython code in order to parse erlang strings (lists of ints)\n",
" * Note that the editing of the IPython code does not affect the normal IPython in any way.\n",
"* ~~**No Variable bindings** (yet)~~\n",
"* ~~Tuples are converted to lists when encoding~~\n",
"* Project structure - it's a mess\n",
"* Not handling all of IPython's messages (yet)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Thanks for the help"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* [Paul Barry](http://glasnost.itcarlow.ie/~barryp/) - Supervisor/Mastermind\n",
"* [Andrew Gibiansky](http://andrew.gibiansky.com/) - IHaskell Creator\n",
"* [Roberto Aloi](http://roberto-aloi.com/) - Erlang Sandbox Creator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Find me on Twitter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##[@LynchRobbie](http://twitter.com/lynchrobbie)"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment