Skip to content

Instantly share code, notes, and snippets.

@jokea
jokea / ex4_2.erl
Created June 20, 2012 04:17
Erlang programming ex4_2
-module(ex4_2).
-export([start/3, stop/0, server/1]).
start(M, N, Message) ->
register(first, spawn(?MODULE, server, [N-1])),
send(M, N, Message).
stop() ->
first ! stop,
ok.
@jokea
jokea / ex3_10.erl
Created June 20, 2012 02:36
[2/2] Erlang programming ex3_10
-module(ex3_10).
-export([format_pretty/2]).
format_pretty(Text, Width) -> do_format(Text, Width, Width).
do_format([], _, _) -> ok;
do_format([H | T], Width, 0) -> display_long_word([H | T], Width, []);
do_format(Text, Width, Request) ->
Len = length(Text),
if
@jokea
jokea / ex3_10.erl
Created June 19, 2012 10:23
[1/2] Erlang programming ex3_10
-module(ex3_10).
-export([format/2]).
format(Text, Width) -> do_format(Text, Width, Width).
do_format([], _, _) -> ok;
do_format(Text, _, Width) when length(Text) < Width -> io:format("~p~n", [Text]);
do_format([H | T], Width, 0) -> display_long_word([H | T], Width, []);
do_format(Text, Width, Request) ->
Len = length(Text),
@jokea
jokea / ex3_9.erl
Created June 19, 2012 08:50
Erlang programming ex3_9
-module(ex3_9).
-export([convert_to_rawdoc/1, convert_raw/1, make_index/1, print/1]).
%%% convert a file into raw document
convert_to_rawdoc(File) ->
case file:open(File, [raw, {read_ahead, 1024}]) of
{error, Reason} ->
io:format("Error opening ~p: ~p~n", [File, Reason]);
{ok, Handle} ->
do_read(Handle, [])
@jokea
jokea / ec.erl
Created June 19, 2012 06:10
Erlang programming ex3_8
%%% restrict: can only handle numbers from 0~9
-module(ec).
-export([parser/1, eval/1, print/1, compile/1, simulate/1, simplify/1]).
parser(List) -> do_parse(List, [], []).
do_parse([], _, [Output]) -> Output;
do_parse(List, Stack, Output) ->
Last = lists:last(List),
Len = length(List),
@jokea
jokea / a.c
Created May 25, 2012 08:12
fork+multithreaded+jemalloc = deadlock?
# cat a.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "zmalloc.h"
void *worker(void *arg) {
int j;
char *p;