<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://bit0.ro/index.xml" rel="self" type="application/atom+xml" /><link href="https://bit0.ro/" rel="alternate" type="text/html" hreflang="en" /><updated>2026-10-25T11:19:16+00:00</updated><id>https://bit0.ro/index.xml</id><title type="html">bits</title><subtitle>bits</subtitle><author><name>ovidiu</name></author><entry><title type="html">Video test</title><link href="https://bit0.ro/2026/09/video-test.html" rel="alternate" type="text/html" title="Video test" /><published>2026-09-10T18:05:37+00:00</published><updated>2026-09-10T18:05:37+00:00</updated><id>https://bit0.ro/2026/09/video-test</id><content type="html" xml:base="https://bit0.ro/2026/09/video-test.html"><![CDATA[<p>Local video serving check. The file lives in <code class="language-plaintext highlighter-rouge">static/media/</code> (symlinked,
git-ignored — 416 MB is too big for git).</p>

<video controls="" preload="metadata" width="100%" src="/media/big_buck_bunny_720p.mp4">
Your browser cannot play HTML5 video.
</video>

<p>Source was a QuickTime <code class="language-plaintext highlighter-rouge">.mov</code> (H.264 + AAC). ffmpeg remuxed it to
MP4 with <code class="language-plaintext highlighter-rouge">+faststart</code> (moov atom first), so playback and seeking
work before the whole ~400 MB file has downloaded. Audio and video
streams were copied, not re-encoded — zero quality loss.</p>]]></content><author><name>ovidiu</name></author><category term="test" /><category term="video" /><summary type="html"><![CDATA[Local video serving check. The file lives in `static/media/` (symlinked, git-ignored — 416 MB is too big for git). Your browser cannot play HTML5 video. Source was a QuickTime `.mov` (H.264 + AAC). ffmpeg remuxed it to…]]></summary></entry><entry><title type="html">tup build system and library manufacturing</title><link href="https://bit0.ro/2019/06/tup-build-system-and-library-manufacturing.html" rel="alternate" type="text/html" title="tup build system and library manufacturing" /><published>2019-06-22T19:35:00+00:00</published><updated>2019-06-22T19:35:00+00:00</updated><id>https://bit0.ro/2019/06/tup-build-system-and-library-manufacturing</id><content type="html" xml:base="https://bit0.ro/2019/06/tup-build-system-and-library-manufacturing.html"><![CDATA[I'm writing this to jot down what is necessary in order to make an automated build process and also debug code as is written based on tup, lldb / gdb pointers and programming in c.<br />
<br />
Basic idea is to have a workflow where feedback is instantanous by the build system, have warning set to maximum, eventually perform lint checks and dynamic memory allocation checks with valgrind.<br />
<br />
These features may already exist in various commercial tools. This is not about that and instead this serves as a my go-to memo to know for future reference.<br />
<br />
1) tup<br />
Tup is a build system. Eeach folder can have it's own tupfile. tuplfile(s) can have&nbsp; global (or specific) tuprules, macros can be defined in tuprules, its an alternative to make, not better but different.<br />
Note:<br />
On linux it uses FUSE to monitor changes in the filesystem Does not work with windows well on this part.<br />
You start the monitor with tup monitor -a and every update, change it will run to the foreground (can also be demonised)<br />
Before wiritng the files you do a tup init in your folder. There it will create a hidden directory similar to the .git directory. There it will store the database that will keep track of dependences, files,and relationships.<br />
<br />
Layout of library project:<br />
<br />
├── Tuprules.tup<br />
├── include&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-here is where function prototypes&nbsp; where #includes<br />
│&nbsp; &nbsp;├── myhash.h&nbsp; &nbsp; &nbsp; &nbsp; are used in the src directory (src depends on include)<br />
│&nbsp; &nbsp;└── mylib.h<br />
├── lib&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-the lib directory is where the archive file is done before the linking<br />
│&nbsp; &nbsp;├── Makefile&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-stage the lib directory is where your library consisting of<br />
│&nbsp; &nbsp;├── Tupfile&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a number of obj files are stred in a single archive file<br />
│&nbsp; &nbsp;└── mylib.a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (lib depends on obj)<br />
├── obj&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -the object file directory is where files are output based<br />
│&nbsp; &nbsp;├── Tupfile&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on the compilation phase. Files which compile should end up here to<br />
│&nbsp; &nbsp;├── myhash.o&nbsp; &nbsp; &nbsp; &nbsp; later be used for the lib folder (obj depends on src)<br />
│&nbsp; &nbsp;├── prime.o<br />
│&nbsp; &nbsp;└── t_hash.o<br />
├── src&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-source files excluding headers (depends on include)<br />
│&nbsp; &nbsp;├── myhash.c<br />
│&nbsp; &nbsp;└── prime.c<br />
├── test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-test directory to make sure everything works as it should.<br />
│&nbsp; &nbsp;├── Makefile&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;This should serve as a place to test all functionality of the library<br />
│&nbsp; &nbsp;├── Tupfile&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and it's functions. Crash&nbsp; tests, should be done here.<br />
│&nbsp; &nbsp;├── t_hash&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(depends on lib, obj - partially)<br />
│&nbsp; &nbsp;├── t_hash.c<br />
└── testdata<br />
<div>
<br /></div>
<div>
Basic syntax for a tupfile</div>
<div>
Rule set situated in the top directory</div>
<div>
<br /></div>
<div>
#Variables start with &amp; - keeping directories here</div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&amp;libdir&nbsp; = include/</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&amp;objdir&nbsp; = obj/</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div>
<div>
<div>
#CFLAGS&nbsp; are appended with +=&nbsp;</div>
</div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">CFLAGS += -Weverything</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">CFLAGS += -g</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">CFLAGS += -Og</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">CFLAGS += -ggdb3</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div>
<div>
<div>
#!macros for use in other Tupfiles - instead of typing the command to archive to compile, etc we can set up a macro</div>
<div>
<br /></div>
</div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">!cc = |&gt; clang $(CFLAGS) -I&amp;(libdir) -c %f -o &amp;(objdir)/%o |&gt; %B.o</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">!ar = |&gt; ar crs %o %f |&gt;</span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
a) <b>lib</b>/Tupfile will include the rules on line 1</div>
<div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">include_rules</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">: ../obj/*.o |&gt; !ar |&gt; mylib.a</span></div>
</div>
<div>
<br /></div>
<div>
Format of the last line is like this: (copy paste from man page)</div>
<div>
<br /></div>
<div>
<div>
&nbsp; : [foreach] [inputs] [ | order-only inputs] |&gt; command |&gt; [outputs] [ | extra outputs] [<group>] [{bin}]</group></div>
<div>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The :-rules are the primary means of creating commands, and are denoted by the fact that the ':' character appears in the first column of the Tupfile. The syntax is&nbsp; supposed to look somewhat like a pipeline, in that the input files on the left go into the command in the middle, and the output files come out on the right.</div>
</div>
<div>
PS:&nbsp;Globbing does not match directories</div>
<div>
<br /></div>
<div>
b) second created Tupfile was in <b>obj</b>/Tupfile</div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">include_rules</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">: foreach ../src/*.c |&gt; !cc |&gt;</span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
The !cc macro specifies the output in the macro, since it is the same for all C files. However, the library can be named anything, so we specify the output in the actual :-rule that uses the !ar macro <br />
<div>
<br /></div>
<div>
c) third file is in tests/Tupfile</div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">include_rules</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">CXFLAGS += -lc</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">CXFLAGS += -lm</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">CXFLAGS += ../lib/mylib.a</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">: *.c |&gt; !cc |&gt; ../obj/t_hash.o</span></div>
<div>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">:&nbsp; ../obj/t_hash.o | ../lib/mylib.a&nbsp; |&gt; clang&nbsp; %f&nbsp; $(CXFLAGS) -o t_hash |&gt; %B</span></div>
</div>
<div>
<br /></div>
<div>
This was more time consuming but the firs rule is to have the t_hash.o file which will become the test has program then have that and the library .a be the input to clang The pipe is "<b>order-only inputs</b><br />
<b><br /></b>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;These are also used as inputs for the command, but will not appear in any of the %-flags. They are separated from regular inputs by use of the '|' character. In effect, these can be used to specify additional inputs to a command that shouldn't appear on the command line. "<br />
<br />
By the way flags:<br />
%f The filename from the "inputs" section. This includes the path and extension.<br />
%o&nbsp; &nbsp; &nbsp;The name of the output file(s)<br />
%B&nbsp; &nbsp; &nbsp;Like %b, but strips the extension.<br />
<br />
Sample output of monitor<br />
<div class="separator" style="clear: both; text-align: center;">
<img tabindex="0" border="0" data-original-height="494" data-original-width="748" height="420" src="/assets/images/tup-build-system-and-library-manufacturing/image-2.png" width="640" /></div>
<br />
<br />
The end strucutre looks like this:<br />
<div class="separator" style="clear: both; text-align: center;">
<img tabindex="0" border="0" data-original-height="672" data-original-width="1600" height="268" src="/assets/images/tup-build-system-and-library-manufacturing/image-4.png" width="640" /></div>
<br />
<br />
2) lldb - more to the point and in a hurry<br />
* breakpoints can't be set on line number as far as i can tell says it's pending<br />
In either case use br set -n function name or br set -r regex for function name<br />
* n for next , s for step<br />
* br l - list breakpoints<br />
* br del - delete them<br />
* me read <br />
<address>
 -fX for hex uppercase -c25 25 lines I think -s8 - for bytes 8 bytes --force if it says it does not display more than 1024 bytes of memory</address>
<br />
* bt to view stack<br />
* frame variables to view what varaibles were assigned and their entry points<br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;"><br /></span>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb)&nbsp;</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">Process 27249 stopped</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">* thread #1, name = 't_hash', stop reason = step over</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp; frame #0: 0x00005555555554bc t_hash`h_search(hTable=0x0000555555559260, key="400") at myhash.c:134:30</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp;131 <span style="white-space: pre;">   </span>else {</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp;132 <span style="white-space: pre;">     </span>// looking for the key inside the next element of the linked list</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp;133 <span style="white-space: pre;">     </span>char* keyAtNextValue = hTable-&gt;h_items[resultHash]-&gt;h_next-&gt;h_key;</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">-&gt; 134 <span style="white-space: pre;">     </span>while( nextValue-&gt;h_next != NULL)&nbsp; {</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp;135 <span style="white-space: pre;">      </span>fprintf(stderr, "Found a collision, traversing Linked list at address %p, key is: %s",</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp;136 <span style="white-space: pre;">        </span> nextValue, nextValue);</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp;137 <span style="white-space: pre;">      </span>nextValue += 1;</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) print nextValue</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(HashNode *) $0 = 0x00005555555592e0</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) br l&nbsp;</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">Current breakpoints:</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">1: name = 'h_search', locations = 1, resolved = 1, hit count = 1</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; 1.1: where = t_hash`h_search + 17 at myhash.c:97:39, address = 0x0000555555555401, resolved, hit count = 1&nbsp;</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) print *nextValue</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(HashNode) $1 = {</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; h_next = 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; h_key = 0x0000555555559300 "400"</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; h_value = 0x0000555555559320 "101"</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">}</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) print *nextValue-&gt;h_next</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">error: Couldn't apply expression side effects : Couldn't dematerialize a result variable: couldn't read its memory</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) print nextValue-&gt;h_next</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(HashNode *) $3 = 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) print *nextValue-&gt;h_key</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char) $4 = '4'</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) print nextValue-&gt;h_key</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char *) $5 = 0x0000555555559300 "400"</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) bt</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">* thread #1, name = 't_hash', stop reason = step over</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; * frame #0: 0x00005555555554bc t_hash`h_search(hTable=0x0000555555559260, key="400") at myhash.c:134:30</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp; frame #1: 0x0000555555555217 t_hash`main at t_hash.c:14:27</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp; frame #2: 0x00007ffff7dd7ee3 libc.so.6`__libc_start_main + 243</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">&nbsp; &nbsp; frame #3: 0x00005555555550fe t_hash`_start + 46</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;"><br /></span>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) me read 0x00005555555592e0 -fX -c25 -s8 --force</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555592e0: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555592e8: 0x0000555555559300&nbsp;</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555592f0: 0x0000555555559320</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555592f8: 0x0000000000000021</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559300: 0x0000000000303034</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559308: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559310: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559318: 0x0000000000000021</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559320: 0x0000000000313031</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559328: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559330: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559338: 0x0000000000000021</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559340: 0x00005555555592E0</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559348: 0x0000555555559360</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559350: 0x0000555555559380</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559358: 0x0000000000000021</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559360: 0x0000000000303034</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559368: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559370: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559378: 0x0000000000000021</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559380: 0x0000000000313031</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559388: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559390: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559398: 0x0000000000020C71</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555593a0: 0x0000000000000000</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) me read 0x00005555555592e0 -c256</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555592e0: 00 00 00 00 00 00 00 00 00 93 55 55 55 55 00 00&nbsp; ..........UUUU..</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555592f0: 20 93 55 55 55 55 00 00 21 00 00 00 00 00 00 00&nbsp; &nbsp;.UUUU..!.......</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559300: 34 30 30 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; 400.............</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559310: 00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00&nbsp; ........!.......</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559320: 31 30 31 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; 101.............</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559330: 00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00&nbsp; ........!.......</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559340: e0 92 55 55 55 55 00 00 60 93 55 55 55 55 00 00&nbsp; ..UUUU..`.UUUU..</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559350: 80 93 55 55 55 55 00 00 21 00 00 00 00 00 00 00&nbsp; ..UUUU..!.......</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559360: 34 30 30 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; 400.............</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559370: 00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00&nbsp; ........!.......</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559380: 31 30 31 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; 101.............</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x555555559390: 00 00 00 00 00 00 00 00 71 0c 02 00 00 00 00 00&nbsp; ........q.......</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555593a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; ................</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555593b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; ................</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555593c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; ................</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">0x5555555593d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00&nbsp; ................</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb)&nbsp;</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;"><br /></span>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;"><br /></span>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">---</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">lldb) frame variable</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(HashTable *) hTable = 0x0000555555559260</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char *) key = 0x0000555555556004 "400"</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(size_t) resultHash = 5</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(HashNode *) nextValue = 0x00005555555592e0</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char *) keyStrAtIndex = <variable available="" not=""></variable></span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;"><br /></span>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char *) keyAtNextValue = <no been="" have="" location="" may="" optimized="" out="" value=""></no></span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;"><br /></span>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(lldb) fr v</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(HashTable *) hTable = 0x0000555555559260</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char *) key = 0x0000555555556004 "400"</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(size_t) resultHash = 5</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(HashNode *) nextValue = 0x00005555555592e0</span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char *) keyStrAtIndex = <variable available="" not=""></variable></span><br />
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;"><br /></span>
<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: xx-small;">(char *) keyAtNextValue = <no been="" have="" location="" may="" optimized="" out="" value=""></no></span></div>
<div>
<br /></div>]]></content><author><name>ovidiu</name></author><summary type="html"><![CDATA[I'm writing this to jot down what is necessary in order to make an automated build process and also debug code as is written based on tup, lldb / gdb pointers and programming in c. Basic idea is to have a workflow where…]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://bit0.ro/assets/images/tup-build-system-and-library-manufacturing/image-2.png" /><media:content medium="image" url="https://bit0.ro/assets/images/tup-build-system-and-library-manufacturing/image-2.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Python Classes</title><link href="https://bit0.ro/2015/05/python-classes.html" rel="alternate" type="text/html" title="Python Classes" /><published>2015-05-25T10:18:00+00:00</published><updated>2015-05-25T10:18:00+00:00</updated><id>https://bit0.ro/2015/05/python-classes</id><content type="html" xml:base="https://bit0.ro/2015/05/python-classes.html"><![CDATA[A short intro on classes in Python with code snippets and notes taken from the python docs<br />
<br />
Class: a blue print for an instance<br />
Instance: a constructed object of the class<br />
Type: indicates the class the instance belongs to<br />
Attribute: any object value : object.attribute<br />
Method: a "callable attribute" defined in the class<br />
<br />
<br />
<!--more--><br />
<script src="https://gist.github.com/ovidiucs/03d4021c94abb0d05b30.js"></script>]]></content><author><name>ovidiu</name></author><category term="python" /><category term="programming" /><summary type="html"><![CDATA[A short intro on classes in Python with code snippets and notes taken from the python docs Class: a blue print for an instance Instance: a constructed object of the class Type: indicates the class the instance belongs…]]></summary></entry><entry><title type="html">Flask pe Raspberry cu Postgres, nginx si uwsgi</title><link href="https://bit0.ro/2015/03/flask-pe-raspberry-cu-postgres-nginx-si-uwsgi.html" rel="alternate" type="text/html" title="Flask pe Raspberry cu Postgres, nginx si uwsgi" /><published>2015-03-29T20:29:00+00:00</published><updated>2015-03-29T20:29:00+00:00</updated><id>https://bit0.ro/2015/03/flask-pe-raspberry-cu-postgres-nginx-si-uwsgi</id><content type="html" xml:base="https://bit0.ro/2015/03/flask-pe-raspberry-cu-postgres-nginx-si-uwsgi.html"><![CDATA[<br />
Raspberry Arch Linux pe ARM. Install guide <a href="http://archlinuxarm.org/platforms/armv6/raspberry-pi" target="_blank">here</a><br />
<br />
Se presupune ca sistemul e conectat la internet.<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">pacman -S nginx python2-virtualenv python-virtualenv uwsgi-plugin-python,uwsgi-plugin-python2 python2-setuptools python2-pip posgtresql bash-completion gcc pithon-pip vim htop atpo sudo nload minicom links tcpdump git tmux ntpd zsh</span><br />
<br />
<!--more-->Comanda de mai sus va instala mai multe pachete decat este necesar dar pot folosi în anumite situații de troubleshooting sau compilarea de pachete. <br />
<br />
Dupa instalarea postgres este necesara logarea cu userul postgres apoi<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'</span><br />
<br />
Si crearea unui user / rol <br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">[postgres]$ createuser --interactive</span><br />
<br />
In nginx.conf din /etc/nginx putem comenta blocul care defineste serverul pentru ca vom declara serverul in alte fisiere .conf pentru un management mai bun al domeniilor virtuale.<br />
<br />
Astfel vom crea o structura a folderelor similara cu ce a apache-ului unde pentru site-urile inacitve exista sites-available si sites-enabled care va contine numai simlink-uri din sites-available. Pentru asta trebuie, pe langa comentarea blocului de server din nginx.conf, adaugam si<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">http {</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; include&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mime.types;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; include sites-enabled/*;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; default_type&nbsp; application/octet-stream;</span><br />
<br />
respectiva optiune pentru ca nginx sa stie de unde va incarca fisierele de config pentru site-uri.<br />
<br />
Tree-ul va arata in felul urmator dupa setup.<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">|-- nginx.conf</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">|-- sites-available</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">|&nbsp;&nbsp; `-- demoapp_nginx.conf</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">|-- sites-enabled</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">|&nbsp;&nbsp; `-- demoapp_nginx.conf -&gt; ../sites-available/demoapp_nginx.conf</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">|-- uwsgi_params</span><br />
<br />
Definirea serverului din <span style="font-family: &quot;Courier New&quot;,Courier,monospace;">demoapp_nginx.conf</span><br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">server {</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; server_name localhost;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; charset&nbsp;&nbsp;&nbsp;&nbsp; utf-8;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; client_max_body_size 75M;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; location / { try_files $uri @yourapplication; }</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; location @yourapplication {</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; include uwsgi_params;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock;</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; }</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">}</span><br />
<br />
In mare descrie pe ce port asculta, unde sa gaseasca fisierele si socketul la care se conecteaza cu uwsgi pentru a crea interfata cu python dat fiind ca nginx este doar un webserver si nu serverste decat continut static.<br />
<br />
Crearea unui serviciu pentru arch pentru systemd e ciudata dar nginx documentează asta pe sit-ul lor. Vezi aici. Il salvezi in /usr/lib/systemd/system/nginx.service .După asta trebuie activat cu comanda systemctl enable nginx.service. Ulterior se poate porni cu comanda systemd start nginx.<br />
<br />
2.<br />
<br />
Se face&nbsp; in /var/www un folder demoapp<br />
Tree-ul final va fi ceva in genul acesta<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">-- demoapp</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; |-- demoapp_uwsgi.ini</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; |-- demoapp_uwsgi.sock</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; |-- hello.py</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; `-- venv</span><br />
<br />
Se da cd in demoapp si se incepe prin crearea containerului virtual care va contine flask, sqlalchemy, psycopg2 si alte trăznăi care fac o aplicație web. Librariile astea e bine sa le ai undeva unde nu va polua sistemul inutil. Pentru asta virtualenv s-a creat.<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">$virtualenv venv</span><br />
<br />
sau<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">$virtualenv2.7 venv</span><br />
<br />
diferența este data de care versiune de python vrei sa lucrezi 3.x sau 2.x<br />
<br />
va crea un folder venv in folderul demoapp. Daca dam comanda <br />
<br />
. venv/bin/activate<br />
<br />
va trece din consola bash într-o noua consola virtuala care ne va permite instalarea de pachete python fara sa polueze sistemul.<br />
<br />
Mediul este izolat. Astfel încât dacă dam comanda virtualenv2.7 și activam consola, când dam comanda python atunci va face referire la python2.7 nu la versiunea instalata pe sistem care este 3.4 atunci când se executa direct python.<br />
<br />
Deci.<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">pip install flask, psycopg, sqlalchemy.</span>Se poate iesi cu comanda deactivate.<br />
<br />
3. <br />
<br />
Se face un fișier hello.py care va conține următorul cod: (se poate crea chiar dacă sunteți in shell-ul virtual)<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">from flask import Flask</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">app = Flask(__name__)</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">@app.route("/")</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">def hello():</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; return "Hello World!"</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">if __name__ == "__main__":</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">&nbsp;&nbsp; app.run(host='0.0.0.0', port=8080)</span><br />
<br />
se salvează fișierul și se pornește webserverul integrat<br />
<br />
python hello.py sau python2.7 hello.py<br />
<br />
apoi ne ducem la adresa pe portul 8080 si ar trebui sa raspunda. <br />
<br />
4. <br />
<br />
Acum ca avem nginx și aplicația web instalata trebuie sa facem sa comunice uwsgi cu cele doua.<br />
<br />
Instalam global <br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">pacman -S uwsgi-plugin-python</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">pacman -S uwsgi-plugin-python2</span><br />
<br />
și definim demoapp_uwsgi.ini<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">[uwsgi]</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">#applications base folder</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">base = /var/www/demoapp</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">#python module to import</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">app = hello</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">module = %(app)</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">plugins = python2</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">home = %(base)/venv</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">pythonpath = %(base)</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">#socket file location</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">socket = /var/www/demoapp/%n.sock</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">#permission for the socket file</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">chmod-socket = 666</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"># the variable that holds a flask application inside the module imported</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">callable&nbsp; = app</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><br /></span>
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">#location of log file</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">logto = /var/log/uwsgi/%n.log</span><br />
<br />
Se pornește cu comanda <br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">uwsgi --plugin python2 --ini /var/www/demoapp/demoapp_uwsgi.ini</span><br />
<br />
opțiunea plugin este doar daca aplicația web folosește python2.Toate bune si frumoase numai ca pornirea uwsgi asa e aiurea. <br />
<br />
Când am instalat uwsgi in /etc/uwsgi un fisier emperor.ini. Mai trebuie sa creeam un folder vassals. Ideea este ca aplicația poate funcționa pe post de superserver care sa spawneze instante pentru cate aplicatii ai, pe langa asta putem porni la startup astfel încât sa avem comunicare cu aplicația si sa nu avem doar webserverul up.<br />
<br />
Folderul vassals va conține un simlink catre fisierul ini care se afla in /var/www/demoapp.<br />
<br />
systemctl start emperor.uwsgi va porni master-ul si slave-ul va porni dacă va exista un fisier de config in vassals.<br />
<br />
&nbsp;]]></content><author><name>ovidiu</name></author><category term="setup. nginx" /><category term="arch" /><category term="dev" /><category term="postgres" /><category term="linux" /><summary type="html"><![CDATA[Raspberry Arch Linux pe ARM. Install guide here Se presupune ca sistemul e conectat la internet. pacman -S nginx python2-virtualenv python-virtualenv uwsgi-plugin-python,uwsgi-plugin-python2 python2-setuptools…]]></summary></entry><entry><title type="html">VLAN-uri</title><link href="https://bit0.ro/2015/03/vlan-uri.html" rel="alternate" type="text/html" title="VLAN-uri" /><published>2015-03-29T09:37:00+00:00</published><updated>2015-03-29T09:37:00+00:00</updated><id>https://bit0.ro/2015/03/vlan-uri</id><content type="html" xml:base="https://bit0.ro/2015/03/vlan-uri.html"><![CDATA[Am vrut sa am doua sisteme sa comunice prin switch pe vlan-uri separate. <br />
Ar fi doua optiuni. <br />
<br />
<ol start="1" type="1">
<li>SVI pe switch</li>
<li>SVI pe linux.</li>
</ol>
Findca sw-ul nu este gbit&nbsp; si raspberry la fel , ma lasa cu desktop-ul cu functia de router. <br />
In sensul acesta trebuie sa facem in felul urmator:<br />
<br />
<!--more--><br />
<br />
<ol start="1" type="1">
<li>Activarea ip.forwarding care by default e off in arch si cel mai probabil in alte distro-uri</li>
<li>Activarea profilelor de conectare daca nu vrem sa folosim script-uri custom
<ol start="1" type="a">
<li>avem profile exemplu in <a class="file" href="https://www.blogger.com/null" title="/etc/netctl/examples">/etc/netctl/examples</a></li>
<li>copiem ce ne interseaza (ethernet-static, vlan-static, si daca avem PPPoE avem si profil pentru el dar prefer sa folosesc direct ppp</li>
<li>sintaxa e foarte stricta si orice greseala duce la neridicarea interfetei si documentatia cam lasa de dorit
<ol start="1" type="1">
<li>exemplu: dupa ridicarea interfetei vreau sa am doua rute, nu doar una, adaugate.Merge cu IPCustom dar nu cu Routes</li>
</ol>
</li>
<li>dupa ce am creat fisierele trebuie activate cu netctl enable numeprofil (profilele le-am denumit dupa numele interfetei)</li>
<li>dupa activare la boot pot fi pornite si let's hope there's no errors reported. </li>
</ol>
</li>
<li>Actualizarea iptables cu o regula POSTROUTING in tabla NAT si asigurarea ca chain-ul de forwarding nu are politica de drop sau daca are atunci sa se adauge exceptie</li>
</ol>
Acum am 3 vlan-uri:<br />
34 - Management 10.34.0.1/29<br />
25 - Raspberry  10.25.0.1/29<br />
86 - Sistem     10.86.0.1/29<br />
<br />
Initial am creat interfete vlan pe sisteme si comunicau daca tineam porturile in trunk. Totusi sisteme care sunt in trunk sunt interesante pentru ca ar permite folosirea de vlan-uri pentru separerea serviciilor de pe acelasi sistem. Am 2 servere pe raspberry. Postgres si Nginx. Le putem da <br />
de pe interfete tagate si am putea discrimina mai bine traficul pe switch. Ramane de incercat.<br />
<br />
As fi vrut si un subnet separat pentru fiecare si un subnet in plus pentru Management-ul prin SSH. <br />
<br />
Pe switch am doua host-uri conectate dar as vrea un svi penru management. Sa ridic un SVI pentru interfete care nu sunt UP  nu merge. Pe loopback nu pot aplica un SVI si vlan state active nu merge ca SVI-ul tot are nevoie de o interfata pe care sa fie traffic. Nu a mers nici cu no keepalive pe o interfata fast.<br />
<br />
Asa ca raman cu optiunea de a folosi loopback care e mereu up si pot accesa echipamentul cat timp am ruta la el. <br />
<br />
Pana la urma am lasat interfata spre sistem trunk si am pus vlan 86 ca nativ. Am vrut&nbsp; o interfata netagata spre switch. <br />
!<br />
interface FastEthernet0/24<br />
switchport trunk encapsulation dot1q<br />
switchport trunk native vlan 86<br />
switchport trunk allowed vlan 25,34<br />
switchport mode trunk<br />
!<br />
<br />
Profiul netctl pentru interfata spre swtich. Foarte important e SkipNoCarrier care nu va ridica interfata da ca nu e link si cand va fi link tot nu o va ridica.<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">cat /etc/netctl/enp4s2
</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">

</span><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">
Description='enp4s2-access 86'<br />
Interface=enp4s2<br />
Connection=ethernet<br />
SkipNoCarrier=yes<br />
IP=static<br />
AutoWired=yes<br />
Address=('10.86.0.2/29')<br />
IPCustom=('route add 10.25.0.0/29 via 10.86.0.1' 'route add 10.34.0.0/29 via 10.86.0.1')&nbsp;</span><br />
<br />
ifconfig va arata interfata dupa netctl start <profil>
</profil><br />
Pe interfata de pe raspberry. Traficul pleaca la fel. Untagged.<br />
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">[root@pytest netctl]# cat eth0<br />
Description='eth0'<br />
Interface=eth0<br />
Connection=ethernet<br />
AutoWired=yes<br />
SkipNoCarrier=yes<br />
IP=static<br />
Address=('10.25.0.2/29')<br />
Gateway=('10.25.0.1')<br />
DNS=('8.8.8.8 8.8.4.4')
</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">

</span><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">
Si pe swtich <br />
!<br />
interface Vlan25<br />
 description Raspberry<br />
 ip address 10.25.0.1 255.255.255.248<br />
!<br />
!<br />
interface FastEthernet0/12<br />
 switchport access vlan 25<br />
 switchport mode access<br />
!
</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">

</span><br />
Cu o ruta statica spre sistemul cu netul
<br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">!<br />
interface Vlan30<br />
 description Calculator<br />
 ip address 10.86.0.1 255.255.255.248<br />
!
</span><br />
<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">

</span><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">
ip route 0.0.0.0 0.0.0.0 10.86.0.2
</span>]]></content><author><name>ovidiu</name></author><category term="VLAN" /><summary type="html"><![CDATA[Am vrut sa am doua sisteme sa comunice prin switch pe vlan-uri separate. Ar fi doua optiuni. SVI pe switch SVI pe linux. Findca sw-ul nu este gbit si raspberry la fel , ma lasa cu desktop-ul cu functia de router. In…]]></summary></entry><entry><title type="html">Keeping track of notes - notepad on steroids</title><link href="https://bit0.ro/2015/02/keeping-track-of-notes-notepad-on-steroids.html" rel="alternate" type="text/html" title="Keeping track of notes - notepad on steroids" /><published>2015-02-21T14:21:00+00:00</published><updated>2015-02-21T14:21:00+00:00</updated><id>https://bit0.ro/2015/02/keeping-track-of-notes-notepad-on-steroids</id><content type="html" xml:base="https://bit0.ro/2015/02/keeping-track-of-notes-notepad-on-steroids.html"><![CDATA[<div class="usertext-body may-blank-within md-container">
<div class="md">
You may like <a class="imgScanned" href="http://zim-wiki.org/screenshots.html" rel="noreferrer" target="_blank">Zimwiki</a><span class="keyNavAnnotation" title="press 1 to open link"> </span>.<br />
<br />
It's a wiki based program that's bare bones, but has the basics: font 
format, headers, bullet lists, number lists, to do boxes.<br />
<br />
<!--more--><br /><br />


Files are just text files stored in the hierarchy of the tree.<br />
<br />


It can export to html. It has some LaTeX templates too.<br />


And it has some decent plugins: spell check, math formula, etc.<br />
<br />
You can have your tree saved to a Dropbox and just point Zim to that notebook
 so everything syncs. If you don't have the program on a computer, then 
you can just open the text file of what you want to edit and do it that 
way.&nbsp;</div>
<div class="md">
&nbsp;</div>
<div class="md">
You can also use - a git repo with bitbucket. Just do a git init on the note folder.&nbsp;</div>
<div class="md">
Bitbucket has private repos and you get the added feature of a version control system. Neat.</div>
<div class="md">
&nbsp;</div>
<div class="md">

PS - its a great program and it's one guy and a few volunteers. I donate when I can because I really rely on this program.<br />
<br />
<br />

</div>
</div>]]></content><author><name>ovidiu</name></author><summary type="html"><![CDATA[You may like Zimwiki . It's a wiki based program that's bare bones, but has the basics: font format, headers, bullet lists, number lists, to do boxes. Files are just text files stored in the hierarchy of the tree. It…]]></summary></entry><entry><title type="html">Connecting a switch to GNS3</title><link href="https://bit0.ro/2015/02/connecting-a-switch-to-gns3.html" rel="alternate" type="text/html" title="Connecting a switch to GNS3" /><published>2015-02-13T11:06:00+00:00</published><updated>2015-02-13T11:06:00+00:00</updated><id>https://bit0.ro/2015/02/connecting-a-switch-to-gns3</id><content type="html" xml:base="https://bit0.ro/2015/02/connecting-a-switch-to-gns3.html"><![CDATA[Connecting a switch to GNS3<br />
VLANs<br />
MAC Table overflow<br />
<!--more--><br />
<hr />
Working setup:<br />
<br />
1 x 3560 L3 Switch<br />
Arch Linux x86_64<br />
GNS 3 v. 1.2.3 w/c7200-adventerprisek9-mz.151-4.M4.image<br />
<br />
<br />
You need to set the options in dynamips executable to allow normal users alterations to network interfaces<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo setcap cap_net_raw,cap_net_admin+eip /usr/bin/dynamips</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>Otherwise<span style="font-family: inherit;">&nbsp;you may receive "</span>206-unable to create Linux raw or generic Ethernet" or something similar.<br />
<span style="font-family: inherit;">More info <a href="http://forum.gns3.net/topic10580.html" target="_blank">here</a></span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
- plug cable into the switch<br />
- make sure you can reach the switch I have:<br />
SW-A# sh int vlan 1<br />
&nbsp; Internet address is 192.168.4.42/24)<br />
- on the NIC you can issue: "<span style="font-family: Courier New, Courier, monospace;">ip addr add 192.168.4.15/24 dev enp4s2</span>" where&nbsp;<span style="font-family: 'Courier New', Courier, monospace;">enp4s2 </span><span style="font-family: inherit;">is&nbsp;</span>whatever<span style="font-family: inherit;">&nbsp;NIC you have</span><br />
<span style="font-family: inherit;">- enable ssh and configure a user</span><br />
<div>
<div>
mig@jenkins ~ % ssh x@192.168.4.42</div>
<div>
Password:&nbsp;</div>
<div>
<br /></div>
<div>
SW-A#</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<img tabindex="0" border="0" src="/assets/images/connecting-a-switch-to-gns3/image-1.png" /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
On R1:</div>
<div class="separator" style="clear: both; text-align: left;">
- set the interface to "duplex full" otherwise you'll receive this on SW-A</div>
<div class="separator" style="clear: both;">
<span style="font-family: Courier New, Courier, monospace;">: %CDP-4-DUPLEX_MISMATCH: duplex mismatch discovered on FastEthernet0/1 (not half duplex), with R1 FastEthernet0/0 (half duplex).</span></div>
<div>
<br /></div>
<div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">SW-A#sh cdp nei</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">Device ID &nbsp; &nbsp; &nbsp; &nbsp;Local Intrfce &nbsp; &nbsp; Holdtme &nbsp; &nbsp;Capability &nbsp;Platform &nbsp;Port ID</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">R1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fas 0/1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 105 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R &nbsp; &nbsp; &nbsp; 7206VXR &nbsp; Fas 0/0</span></div>
</div>
</div>
<div>
<br />
VLANs<br />
<hr />
<br />
Prepare interface on switch:<br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">interface FastEthernet0/1</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">&nbsp;switchport trunk encapsulation dot1q</span><br />
<span style="font-family: Courier New, Courier, monospace;">&nbsp;switchport mode trunk</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: inherit;">DTP packets start arriving</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<img tabindex="0" border="0" src="/assets/images/connecting-a-switch-to-gns3/image-2.png" height="490" width="640" /></div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><br /></span>
<span style="font-family: inherit;">Interface is now trunk so it does not show on this output</span><br />
<span style="font-size: x-small;"><br /></span>
SW-A#sh vlan br<br />
<br />
VLAN Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Status &nbsp; &nbsp;Ports<br />
---- -------------------------------- --------- -------------------------------<br />
<br />
1 &nbsp; &nbsp;default &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;active &nbsp; &nbsp;Fa0/2, Fa0/3, Fa0/4, Fa0/5<br />
<div>
<br /></div>
To see trunk interfaces:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">SW-A#sh int trunk&nbsp;</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">Port &nbsp; &nbsp; &nbsp; &nbsp;Mode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Encapsulation &nbsp;Status &nbsp; &nbsp; &nbsp; &nbsp;<b>Native</b> vlan</span><br />
<span style="font-family: Courier New, Courier, monospace;">Fa0/1 &nbsp; &nbsp; &nbsp; on &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 802.1q &nbsp; &nbsp; &nbsp; &nbsp; trunking &nbsp; &nbsp; &nbsp;<b>1</b></span><br />
<div>
<br /></div>
Configure a SVI<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">SW-A(config)#vlan 100</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><i>00:51:25: %LINK-5-CHANGED: Interface Vlan100, changed state to administratively down</i></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><i>00:51:26: %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan100, changed state to down</i></span><br />
<span style="font-family: Courier New, Courier, monospace;">SW-A(config)#int vlan 100 &nbsp; &nbsp; &nbsp;&nbsp;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><i>00:51:33: %LINK-3-UPDOWN: Interface Vlan100, changed state to up</i></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><i>00:51:34: %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan100, changed state to up</i></span><br />
<span style="font-family: Courier New, Courier, monospace;">SW-A(config-if)#ip addr 10.1.1.1 255.255.255.240&nbsp;</span><br />
<div>
<br /></div>
</div>
<div>
<br /></div>
<div>
On the PC side:</div>
<div>
- Create the VLAN device</div>
<div>
<span style="font-family: Courier New, Courier, monospace;">ip link add link&nbsp;</span><span style="font-family: 'Courier New', Courier, monospace;">enp4s2</span><span style="font-family: Courier New, Courier, monospace;">&nbsp;name&nbsp;</span><span style="font-family: 'Courier New', Courier, monospace;">enp4s2</span><span style="font-family: Courier New, Courier, monospace;">.100 type vlan id 100</span></div>
<div>
- add an ip&nbsp;</div>
<div>
<span style="font-family: Courier New, Courier, monospace;">ip addr add&nbsp;10.1.1.1/30 brd&nbsp;10.1.1.3&nbsp;dev&nbsp;enp4s2.100</span></div>
<div>
<span style="font-family: inherit;">- bring it up</span><span style="font-family: Courier New, Courier, monospace;">&nbsp;</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">ip link set dev&nbsp;</span><span style="font-family: 'Courier New', Courier, monospace;">enp4s2.100</span><span style="font-family: Courier New, Courier, monospace;">&nbsp;up</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">You can check the status by:</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">&nbsp;~ % ip -d link show enp4s2 &nbsp; &nbsp;</span><br />
<span style="font-family: Courier New, Courier, monospace;">3: enp4s2: <broadcast> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000</broadcast></span><br />
<span style="font-family: Courier New, Courier, monospace;">&nbsp; &nbsp; link/ether 00:22:15:63:cf:0d brd ff:ff:ff:ff:ff:ff promiscuity 1&nbsp;</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">&nbsp;~ % ip -d link show enp4s2.100</span><br />
<span style="font-family: Courier New, Courier, monospace;">5: enp4s2.100@enp4s2: <no-carrier> mtu 1500 qdisc noqueue state LOWERLAYERDOWN mode DEFAULT group default&nbsp;</no-carrier></span><br />
<span style="font-family: Courier New, Courier, monospace;">&nbsp; &nbsp; link/ether 00:22:15:63:cf:0d brd ff:ff:ff:ff:ff:ff promiscuity 0&nbsp;</span><br />
<span style="font-family: Courier New, Courier, monospace;"></span><br />
<span style="font-family: Courier New, Courier, monospace;">&nbsp; &nbsp; <b>vlan protocol 802.1Q id 100</b> <reorder_hdr>&nbsp;</reorder_hdr></span><br />
<span style="font-family: Courier New, Courier, monospace;"><reorder_hdr><br /></reorder_hdr></span>
<span style="background-color: white;">A virtual network device called&nbsp;</span><span style="font-family: 'Courier New', Courier, monospace;">enp4s2.100</span><span style="background-color: white;">&nbsp;would be created, bound to VLAN ID 100 of the physical interface&nbsp;</span><span style="font-family: 'Courier New', Courier, monospace;">enp4s2</span><br />
<ul style="background-color: white;">
<li>Inbound 802.1Q-encapsulated frames arriving on&nbsp;<span style="font-family: 'Courier New', Courier, monospace;">enp4s2</span>&nbsp;with a VLAN ID of 100 are detagged, then re-presented to the network stack as inbound frames arriving on&nbsp;<span style="font-family: 'Courier New', Courier, monospace;">enp4s2.100</span></li>
<li>Outbound frames sent to&nbsp;<span style="font-family: 'Courier New', Courier, monospace;">enp4s2.100</span>&nbsp;are tagged with a VLAN ID of 100, then passed to&nbsp;<span style="font-family: 'Courier New', Courier, monospace;">enp4s2</span>&nbsp;for transmission.</li>
</ul>
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<br /></div>]]></content><author><name>ovidiu</name></author><summary type="html"><![CDATA[Connecting a switch to GNS3 VLANs MAC Table overflow Working setup: 1 x 3560 L3 Switch Arch Linux x86_64 GNS 3 v. 1.2.3 w/c7200-adventerprisek9-mz.151-4.M4.image You need to set the options in dynamips executable to…]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://bit0.ro/assets/images/connecting-a-switch-to-gns3/image-1.png" /><media:content medium="image" url="https://bit0.ro/assets/images/connecting-a-switch-to-gns3/image-1.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Haskell Introducere, Liste, uplu, functii, tipuri polimorfice, supraincarcare.</title><link href="https://bit0.ro/2014/10/haskell-introducere-liste-uplu-functii-tipuri-polimorfice-supraincarcare.html" rel="alternate" type="text/html" title="Haskell Introducere, Liste, uplu, functii, tipuri polimorfice, supraincarcare." /><published>2014-10-26T17:21:00+00:00</published><updated>2014-10-26T17:21:00+00:00</updated><id>https://bit0.ro/2014/10/haskell-introducere-liste-uplu-functii-tipuri-polimorfice-supraincarcare</id><content type="html" xml:base="https://bit0.ro/2014/10/haskell-introducere-liste-uplu-functii-tipuri-polimorfice-supraincarcare.html"><![CDATA[<h3>
Tipurile de baza:</h3>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">Bool</span> – valori logice</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify;">
Contine
valorile logice <span class="CodeChar">False</span> si <span class="CodeChar">True</span>.</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">Char</span> – caractere</div>
<div class="MsoNormal" style="margin-left: 54.0pt;">
Contine toate caracterele
disponibile de la tastatura precum <span class="CodeChar">‘a’</span>, <span class="CodeChar">‘_’</span>, <span class="CodeChar">‘V’</span>, precum si un numar
de caractere de control precum <span class="CodeChar">‘\n’</span>, <span class="CodeChar">‘\t’</span>.</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">String</span> – siruri de caractere</div>
<div class="MsoNormal" style="margin-left: 54.0pt;">
Acest tip contine toate
secventele de caractectere, precum <span class="CodeChar">“abc”</span>, <span class="CodeChar">“1+2=3”</span> si sirul vid <span class="CodeChar">“”</span>.</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">Int</span> – numere intregi intre <span class="CodeChar">-2<sup>31</sup> </span>si <span class="CodeChar">2<sup>31 </sup>-1</span></div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">Integer</span> – numere intregi cu precizie
infinita</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">Float</span> - single-precision
floating-point numbers</div>
<div class="MsoNormal">
<br />
<!--more--><br /></div>
<h3>
Tipul lista:</h3>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>Secventa(chiar si infinita) de elemente de acelasi tip:</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>[True, False, False] :: [Bool]</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>[‘a’,’b’,’c’,’d’] :: [Char]</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>[“unu”, “doi”, “trei”] :: String</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>[[]] este diferita de [] </div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>nu exista restrictii asupra tipurilor ce pot forma liste!</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>[[‘a’, ‘b’], [‘a’, ‘c’, ‘d’]] :: [[Char]]</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>3:(2:(1:[])) , lista construita cu ‘cons’.</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>datorita evaluarii tarzii listele infinite sunt posibile in
Haskell.</div>
<div class="MsoNormal" style="margin-left: 18.0pt;">
<br /></div>
<h3>
Tipul uplu:</h3>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>secventa finita de componente, posibil de tipuri diferite:</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>(False, True) :: (Bool, Bool)</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>(False, ‘a’, True) :: (Bool, Char, Bool)</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>(“Yes”, True, ‘a’) :: (String, Bool, Char)</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>numarul de componente dintr-un uplu se numeste aritate</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>uple de aritate 1 precum (False) nu sunt permise deoarece intra
in conflict cu folosirea parantezelor pentru a forta ordinea unei evaluari. Ex.
(1+2)*3</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>tipul unui uplu ii denota aritatea</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>(Bool, Bool)&nbsp; ulpu de aritate 2</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>(String, Bool, Char) ulpu de aritate 3</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>nu exista restrictii asupra tipurilor ce pot forma uple:</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>(’a’, (False, ’b’)) :: (Char, (Bool , Char))</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>([’a’, ’b’], [False, True ]) :: ([Char ], [Bool ])</div>
<div class="MsoNormal">
<br /></div>
<h3>
Tipul functie:</h3>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>functia este o mapare de la argumente de un tip la rezultate de alt
tip.</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">T­<sub>1</sub> -&gt; T</span><span class="CodeChar"><sub><span style="font-family: b;">2</span></sub></span> este
tipul tuturor functiilor ce mapeaza argumente de tipul T<sub>1</sub> la
rezultate de tipul T<sub>2:</sub></div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span class="CodeChar"><span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><span class="CodeChar">isDigit::Char-&gt;Bool</span></div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span class="CodeChar"><span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><span class="CodeChar">and::[Bool]-&gt;Bool</span></div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>nu exista restrictia ca functiile sa fie totale (sa fie definite
ptr orice argument valid). Functia de biblioteca <span class="CodeChar">tail</span>
nu e definita ptr lista vida.</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>In Haskell e standard ca functiile sa fie precedate de declaratia
de tip, care e verificata la compilare cu cea obtinuta prin <i>type inference</i>.</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>functiile cu mai multe argumente se pot defini in 2 feluri:</div>
<div class="MsoNormal" style="margin-left: 90.0pt; text-indent: -18.0pt;">
<span style="font-family: &quot;Courier New&quot;;">o<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>“impachetand” argumentele in uple sau liste:</div>
<div class="Code" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>add :: (Int, Int) -&gt; Int</div>
<div class="Code" style="margin-left: 126.0pt;">
add (x,y) = x + y</div>
<div class="Code" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>sum :: [Int] -&gt; Int</div>
<div class="Code" style="margin-left: 126.0pt;">
sum x:xs = x + sum xs</div>
<div class="MsoNormal" style="margin-left: 90.0pt; text-indent: -18.0pt;">
<span style="font-family: &quot;Courier New&quot;;">o<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>prin functii Curry (metoda nativa Haskell):</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>sunt functii ce intorc alte functii</div>
<div class="Code" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>add’ :: Int-&gt;(Int-&gt;Int)</div>
<div class="Code" style="margin-left: 126.0pt;">
add’ x y = x + y</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>declaratia functiei ar fi identica chiar si fara paranteze, fiind
asociativa la dreapta.</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>definitia functiei e asociativa la stanga: </div>
<div class="MsoNormal" style="margin-left: 162.0pt; text-indent: -18.0pt;">
<span style="font-family: &quot;Courier New&quot;;">o<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>((add’ x ) y) = add’ x y</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>in afara de cazurile in care trimiterea parametrilor sub forma de
uple e strict necesara, toate functiile in Haskell cu parametri multipli sunt
definite ca functii curry.</div>
<div class="MsoNormal">
<br /></div>
<h3>
Tipuri polimorfice:</h3>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>functiile pot fi definite pentru mai multe tipuri: functia <span class="CodeChar">length</span> trebuie sa poata fi aplicata pe orice tip de
lista!</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>este posibil prin definirea functiei pe <i>tipuri variabile:</i></div>
<div class="Code" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>length :: [a] -&gt; Int</div>
<div class="Code" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>head :: [a] -&gt; a</div>
<div class="Code" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>id :: a -&gt; a</div>
<div class="Code" style="margin-left: 126.0pt; text-indent: -18.0pt;">
<span style="font-family: Wingdings; font-weight: normal;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>fst :: (a,b) -&gt; a</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>variabilele de tip incep prin litere mici si se noteaza de
obicei: a, b, c</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>un tip ce contine unul sau mai multe tipuri/variabile de tip se
numeste <i>polimorfic.</i></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<h3>
Tipuri supraincarcate:</h3>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>operatorii <span class="CodeChar">+, -, *, ^</span> se aplica mai
multor tipuri</div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>tipul acestora contine variabile de tip supuse unor constrangeri
de clasa ce se scriu sub forma:</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>&nbsp;<span class="CodeChar">C a</span> unde <span class="CodeChar">C</span>
este numele clasei si <span class="CodeChar">a</span> variabila de tip</div>
<div class="MsoNormal" style="margin-left: 126.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Wingdings;">§<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span class="CodeChar">(+) :: <i>Num</i> a =&gt; a-&gt; a-&gt;a</span>
adica pentru orice tip <span class="CodeChar">a</span>, instanta a clasei <span class="CodeChar"><i>Num</i></span>, functia <span class="CodeChar">(+)</span> are
tipul <span class="CodeChar">a-&gt;a-&gt;a</span></div>
<div class="MsoNormal" style="margin-left: 54.0pt; text-align: justify; text-indent: -18.0pt;">
<span style="font-family: Symbol;">·<span style="font: 7.0pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span>acestea se numesc tipuri <i>supraincarcate</i></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>]]></content><author><name>ovidiu</name></author><summary type="html"><![CDATA[Tipurile de baza: · Bool – valori logice Contine valorile logice False si True . · Char – caractere Contine toate caracterele disponibile de la tastatura precum ‘a’ , ‘_’ , ‘V’ , precum si un numar de caractere de…]]></summary></entry><entry><title type="html">Haskell - Defining custom data types</title><link href="https://bit0.ro/2014/10/haskell-defining-custom-data-types.html" rel="alternate" type="text/html" title="Haskell - Defining custom data types" /><published>2014-10-23T08:25:00+00:00</published><updated>2014-10-23T08:25:00+00:00</updated><id>https://bit0.ro/2014/10/haskell-defining-custom-data-types</id><content type="html" xml:base="https://bit0.ro/2014/10/haskell-defining-custom-data-types.html"><![CDATA[<ul>
<li>allows us to add structure to the values in our programs</li>
<li>defining our own types also imrpoves the safety of our code</li>
<li>haskell will not allow us to accidentally mix values of two values that are structurally similar but have different names.</li>
</ul>
<br />
<pre class="programlisting" id="BookStore.hs:BookInfo">-- file: ch03/BookStore.hs
data BookInfo = Book Int String [String]
                deriving (Show)</pre>
<pre class="programlisting" id="BookStore.hs:BookInfo">&nbsp;</pre>
<ol>
<li>The <span class="type">BookInfo</span> after the
      <code class="code">data</code> keyword is the name of our new type. We call
      <span class="type">BookInfo</span> a <span class="emphasis"><i>type constructor</i></span>.&nbsp;</li>
<li>The <code class="code">Book</code> that follows is the name of the
      <span class="emphasis"><i>value constructor</i></span> (sometimes called a data
      constructor)</li>
<li>After <code class="code">Book</code>, the <span class="type">Int</span>,
      <span class="type">String</span>, and <span class="type">[String]</span> that follow are
      the <span class="emphasis"><em>components</em></span> of the type.</li>
</ol>
<!--more-->]]></content><author><name>ovidiu</name></author><summary type="html"><![CDATA[allows us to add structure to the values in our programs defining our own types also imrpoves the safety of our code haskell will not allow us to accidentally mix values of two values that are structurally similar but…]]></summary></entry><entry><title type="html">Haskell Evaluation</title><link href="https://bit0.ro/2014/10/haskell-evaluation.html" rel="alternate" type="text/html" title="Haskell Evaluation" /><published>2014-10-21T05:38:00+00:00</published><updated>2014-10-21T05:38:00+00:00</updated><id>https://bit0.ro/2014/10/haskell-evaluation</id><content type="html" xml:base="https://bit0.ro/2014/10/haskell-evaluation.html"><![CDATA[<div class="titlepage">
<div>
<div>
<h3 class="title" id="funcstypes.if">
Conditional evaluation </h3>
</div>
</div>
</div>
<pre class="programlisting" id="myDrop.hs:myDrop">-- file: ch02/myDrop.hs
myDrop n xs = if <span style="color: #6aa84f;"><b>n</b></span> &lt;= 0 || null xs
              then <b><span style="color: #f1c232;">xs</span></b>
              else myDrop (n - 1) (tail xs)&nbsp; </pre>
The <b><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">if</span></b> keyword introduces an expression that has three components.<br />
<ul>
<li>An expression of type Bool, immediately following the <span style="font-family: &quot;Courier New&quot;,Courier,monospace;">if</span>. We refer to this as a <b><u>predicate</u></b>. </li>
<li>A <b><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">then</span></b> keyword, followed by another expression. This expression will be used as the value of the if expression if the predicate evaluates to <b><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">True</span></b>. </li>
<li>An <span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><b>else</b></span> keyword, followed by another expression. This expression will be used as the value of the <b><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">if</span></b> expression if the predicate evaluates to <b><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">False</span></b>.</li>
<li>The <b><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">null</span></b> function indicates whether a list is empty, while the (<span style="font-family: &quot;Courier New&quot;,Courier,monospace;"><b>||</b></span>) operator performs a logical “or” of its Bool-typed arguments.&nbsp;</li>
</ul>
<blockquote class="tr_bq">
<pre class="screen" id="myDrop.ghci:null"><code class="prompt">ghci&gt; </code><strong class="userinput"><code>:type null</code></strong>
null :: [a] -&gt; Bool
<code class="prompt">ghci&gt; </code><strong class="userinput"><code>:type (||)</code></strong>
(||) :: Bool -&gt; Bool -&gt; Bool
</pre>
</blockquote>
<br />
<br />
<!--more-->Operators are not special<a href="https://www.blogger.com/null" name="x_Be"></a>.Notice that we were able to find the type of
   <code class="function">(||)</code> by wrapping it in parentheses. The
   <code class="function">(||)</code> operator isn't “<span class="quote">built
     into</span>” the language: it's an ordinary
   function.<br />
<br />
The following are equivalent<br />
<br />
<b><span style="font-family: &quot;Courier New&quot;,Courier,monospace;">True || False<br />(||) True False</span></b><br />
<br />
<br />
<ul>
</ul>
We'll refer to the expressions after the <span style="font-family: &quot;Courier New&quot;,Courier,monospace;">then</span>&nbsp; and&nbsp;<span style="font-family: &quot;Courier New&quot;,Courier,monospace;">else</span> keywords as
 “<u><span class="quote">branches</span></u>”. The branches must have the same
 types; the <code class="literal">if</code> expression will also have this type.  An
 expression such as <code class="code">if True then 1 else "foo"</code> has
 different types for its branches, so it is<u> ill typed </u>and will
 be rejected by a compiler or interpreter.<br />
<br />
In an imperative language, it can make sense to
 omit the <code class="code">else</code> branch from an <code class="code">if</code>,
 because we're working with <span class="emphasis"><i>statements</i></span>,
 not <b>expressions</b>.  However, when we're working with
 expressions, an <code class="code">if</code> that was missing an
 <code class="code">else</code> wouldn't have a result or type if the
 predicate evaluated to <code class="function">False</code>, so it would
 be nonsensical. <br />
<br />
<span class="comment" id="comments_x_s3"> It is an if *expression* 
rather than an if *statement*.  This is an important distinction and 
worth stressing. An <span style="color: red;">if expression</span> <b>must evaluate to a value</b>, whereas an 
if statement can leave off the else clause as a shorthand for the 'do 
nothing' action.  It still would not make sense to leave off the 'else' 
clause from an if-expression in a dynamically typed language.</span><br />
<span class="comment" id="comments_x_s3">&nbsp;</span> <br />
Our procedure will involve rewriting expressions over and
      over, substituting expressions for variables until we reach a
      final result.<br />
<div class="titlepage">
<div>
<div>
<h3 class="title" id="id580456">
Lazy evaluation</h3>
In a language that uses strict evaluation, the arguments to a function are evaluated before the function is applied. Haskell chooses another path: non-strict evaluation.<br />
<br />
In Haskell, the subexpression 1 + 2 is not reduced to the value 3. Instead, we create a “promise” that when the value of the expression isOdd (1 + 2) is needed, we'll be able to compute it. The record that we use to track an unevaluated expression is referred to as a thunk --&nbsp;<span class="comment" id="comments_x_fm"> A thunk is a function which takes no arguments. -- </span><span class="comment" id="comments_x_fm">a delayed computation --</span> This is all that happens: we create a thunk, and defer the actual evaluation until it's really needed. If the result of this expression is never subsequently used, we will not compute its value at all.<br />
<br />
Non-strict evaluation is often referred to as lazy evaluation<br />
<br />
<br />
<ul type="disc">
<li><div id="x_Ze">
It makes sense to use substitution and rewriting to
     understand the evaluation of a Haskell expression.<span class="comment" id="comments_x_Ze"><a class="commenttoggle" href="comment:%20add" id="toggle_x_Ze"> </a></span></div>
</li>
<li><div id="x_ae">
<a href="https://www.blogger.com/null" name="x_ae"></a>Laziness leads us to defer evaluation until we need a
     value, and to evaluate just enough of an expression to
     establish its value.<span class="comment" id="comments_x_ae"><a class="commenttoggle" href="comments:%20show%20/%20hide" id="toggle_x_ae"> </a></span></div>
</li>
<li><div id="x_be">
<a href="https://www.blogger.com/null" name="x_be"></a>The result of applying a function may be a
     thunk (a deferred expression)</div>
</li>
</ul>
</div>
</div>
</div>
<br />
<mytubeelement data="{&quot;bundle&quot;:{&quot;label_delimitor&quot;:&quot;:&quot;,&quot;percentage&quot;:&quot;%&quot;,&quot;smart_buffer&quot;:&quot;Smart Buffer&quot;,&quot;start_playing_when_buffered&quot;:&quot;Start playing when buffered&quot;,&quot;sound&quot;:&quot;Sound&quot;,&quot;desktop_notification&quot;:&quot;Desktop Notification&quot;,&quot;continuation_on_next_line&quot;:&quot;-&quot;,&quot;loop&quot;:&quot;Loop&quot;,&quot;only_notify&quot;:&quot;Only Notify&quot;,&quot;estimated_time&quot;:&quot;Estimated Time&quot;,&quot;global_preferences&quot;:&quot;Global Preferences&quot;,&quot;no_notification_supported_on_your_browser&quot;:&quot;No notification style supported on your browser version&quot;,&quot;video_buffered&quot;:&quot;Video Buffered&quot;,&quot;buffered&quot;:&quot;Buffered&quot;,&quot;hyphen&quot;:&quot;-&quot;,&quot;buffered_message&quot;:&quot;The video has been buffered as requested and is ready to play.&quot;,&quot;not_supported&quot;:&quot;Not Supported&quot;,&quot;on&quot;:&quot;On&quot;,&quot;off&quot;:&quot;Off&quot;,&quot;click_to_enable_for_this_site&quot;:&quot;Click to enable for this site&quot;,&quot;desktop_notification_denied&quot;:&quot;You have denied permission for desktop notification for this site&quot;,&quot;notification_status_delimitor&quot;:&quot;;&quot;,&quot;error&quot;:&quot;Error&quot;,&quot;adblock_interferance_message&quot;:&quot;Adblock (or similar extension) is known to interfere with SmartVideo. Please add this url to adblock whitelist.&quot;,&quot;calculating&quot;:&quot;Calculating&quot;,&quot;waiting&quot;:&quot;Waiting&quot;,&quot;will_start_buffering_when_initialized&quot;:&quot;Will start buffering when initialized&quot;,&quot;will_start_playing_when_initialized&quot;:&quot;Will start playing when initialized&quot;,&quot;completed&quot;:&quot;Completed&quot;,&quot;buffering_stalled&quot;:&quot;Buffering is stalled. Will stop.&quot;,&quot;stopped&quot;:&quot;Stopped&quot;,&quot;hr&quot;:&quot;Hr&quot;,&quot;min&quot;:&quot;Min&quot;,&quot;sec&quot;:&quot;Sec&quot;,&quot;any_moment&quot;:&quot;Any Moment&quot;,&quot;popup_donate_to&quot;:&quot;Donate to&quot;,&quot;extension_id&quot;:null},&quot;prefs&quot;:{&quot;desktopNotification&quot;:true,&quot;soundNotification&quot;:false,&quot;logLevel&quot;:0,&quot;enable&quot;:true,&quot;loop&quot;:false,&quot;hidePopup&quot;:false,&quot;autoPlay&quot;:false,&quot;autoBuffer&quot;:false,&quot;autoPlayOnBuffer&quot;:false,&quot;autoPlayOnBufferPercentage&quot;:42,&quot;autoPlayOnSmartBuffer&quot;:true,&quot;quality&quot;:&quot;default&quot;,&quot;fshd&quot;:false,&quot;onlyNotification&quot;:false,&quot;enableFullScreen&quot;:true,&quot;saveBandwidth&quot;:false,&quot;hideAnnotations&quot;:false,&quot;turnOffPagedBuffering&quot;:false}}" event="preferencesUpdated" id="myTubeRelayElementToPage"></mytubeelement><mytubeelement data="{&quot;loadBundle&quot;:true}" event="relayPrefs" id="myTubeRelayElementToTab"></mytubeelement>]]></content><author><name>ovidiu</name></author><category term="haskell" /><category term="programming" /><summary type="html"><![CDATA[Conditional evaluation -- file: ch02/myDrop.hs myDrop n xs = if n <= 0 || null xs then xs else myDrop (n - 1) (tail xs) The if keyword introduces an expression that has three components. An expression of type Bool…]]></summary></entry></feed>