Skip to content

Instantly share code, notes, and snippets.

View mareknowak's full-sized avatar

Marek Nowak mareknowak

  • Poland
View GitHub Profile
@mareknowak
mareknowak / index.erl
Created May 23, 2020 17:49
Programming challenge: indexing a file - FP in Erlang 3.3
%% FP in Erlang 3.3
%%
%% Programming challenge: indexing a file
-module(index).
-export(
[ get_file_contents/1
, show_file_contents/1
, split_index/2
, split_index_bin/2
@mareknowak
mareknowak / morefuns.erl
Last active May 19, 2020 04:20
More functions over lists - FP in Erlang 2.27
%% More functions over lists
%% FP in Erlang 2.27
-module(morefuns).
-export(
[ join/2
, concat/1
, member/2
, mergesort/1
, quicksort/1
@mareknowak
mareknowak / reclists.erl
Created May 14, 2020 18:23
Constructing lists with recursive functions - FP in Erlang 2.18
%% FP in Erlang 2.18
%% Constructing lists with recursive functions
%%
-module(reclists).
-export(
[ double/1
, even/1
% , divide/4
% , sort/1
@mareknowak
mareknowak / funlists.erl
Last active May 13, 2020 10:29
Functions over lists - FP in Erlang 2.15
%% FP in Erlang 2.15
%% Defining functions over lists in practice
-module(funlists).
-export(
[ product/1 % tail recursive version
, product_direct/1 % direct version
, maximum/1 % tail recursive version
, maximum_direct/1 % direct version
, maximum_brujo/1 % both direct and tail recursive
@mareknowak
mareknowak / tail.erl
Last active May 9, 2020 03:27
Tail recursion - FP in Erlang 2.5
%% @doc Tail recursion - FP in Erlang 2.5
-module(tail).
-export([sum/2, maximum/2, fib/1, perfect/1, test/0]).
%% @doc Given a function (Fun: int -> int) and an integer N,
%% find the value of Fun(N) + Fun(N - 1) + ... + Fun(0).
%%
%% sum_aux is auxiliary function keeping a current Sum value
@mareknowak
mareknowak / recursion.erl
Created May 8, 2020 04:39
Recursion examples - FP in Erlang 2.3
%% @doc Recursion examples - FP in Erlang 2.3
-module(recursion).
-export([fib/1, pieces/1, test/0]).
%% @doc Give N-th element of Fibonacci sequence
fib(0) -> 0;
fib(1) -> 1;
fib(N) when N > 1 ->
@mareknowak
mareknowak / patterns.erl
Last active May 7, 2020 08:23
Practice patterns (1.15)
%% @doc Practice patterns (1.15)
-module(patterns).
-export([x_or_1/2, x_or_2/2, x_or_3/2, max/2, max_three/3, how_many_equal/3, test/0]).
%% @doc Give three "exclusive ors"
x_or_1(X, Y) ->
X =/= Y.
%% @doc Functions for calculating perimeter and area of a right angled triangle
-module(second).
-export([hypotenuse/2, perimeter/2, area/2]).
hypotenuse(A, B) ->
math:sqrt(first:square(A) + first:square(B)).
%% @doc Perimeter of a right angled triangle
%% A, B - lengths of shorter sides
%% @doc Function for calculating area of any triangle and some auxiliary
%% functions
-module(first).
-export([area/3, double/1, mult/2, square/1, treble/1]).
mult(X, Y) ->
X * Y.
double(X) ->