Skip to content

Instantly share code, notes, and snippets.

View pppillai's full-sized avatar

Pradeep Prabhakaran Pillai pppillai

View GitHub Profile
-module(first).
-export([double/1, mult/2, area/3, squareValue/1, tripleValue/1]).
mult(X, Y) ->
X * Y.
double(X) ->
mult(X, 2).
-module(second).
-import(first, [squareValue/1]).
-export([sizeOfHypotenuse/2, perimeterOfRightAngledTriangle/2, areaOfTriangle/2]).
%%below functions to be used for right angled triangle
%returns the hypotenuse
sizeOfHypotenuse(SideOne, SideTwo) ->
math:sqrt(squareValue(SideOne) + squareValue(SideTwo)).
-module(week1).
-export([xOr_first_impl/2, xOr_second_impl/2, xOr_third_impl/2, maxThree/3, howManyEqual/3
, test_howManyEqual_function/0, test_maxThree_function/0, test_xOrImpl_function/0]).
%xOr implementations.
xOr_first_impl(A, B) ->
A =/= B.
-module(fibandcuts).
-export([factorial/1, fibonacci/1, pieces/1]).
-export([test_pieces/0,test_fibonacci/0]).
-author("Pradeep Pillai").
%Factorial of N
%1 * 2 * 3 * (N-1)
%Factorial of 0 is 0
-module(week2_perfect_number).
-export([perfectNumber/1]).
-author("Pradeep Pillai").
perfectNumber(N) ->
A = doPerfect(N, N div 2, []),
N == lists:sum(A).
doPerfect(_, 0, Result) ->
-module(tailrecursion).
-export([loop/1, fib/1, perfect/1, test_perfect_number/0, test_fib/0]).
loop(N) when N > 0 ->
io:format("~p~n", [N]),
loop(N-1);
loop(_N) ->
io:format("bye ~n").
-module(four_arg_fib).
-export([fib/1, test_fib/0]).
%four argument fib function
fib(N) ->
fib(0, 0, 1, N).
fib(From, Current, _Next, From) ->
Current;
fib(From, Current, Next, To) ->
fib(From+1, Next, Current + Next, To).
-module('chapter-5').
-export([map_search_pred/2]).
% write a funciton map_search(Map, Pred) that returns the first element {Key, Value}
% in the map for which Pred(Key, Value) is true.
% 16> A = fun(X, Y) -> case X > Y of true -> true; _ -> false end end.
% #Fun<erl_eval.13.126501267>
% 17> A(1, 2).
% false
% 18> 'chapter-5':map_search_pred(#{1=>2, 4=>3}, A).
-module(countchar).
-export([count_characters/1]).
count_characters(Str) ->
count_characters(Str, #{}).
count_characters([], Result) ->
Result;
count_characters([H|T], Result) ->
case maps:is_key(H, Result) of
-module(take).
-export([take/2, test_take/0]).
% Define a function take that takes the first N elements from a list. Here are some examples of take in action:
% take(0,"hello") = []
% take(4,"hello") = "hell"
% take(5,"hello") = "hello"
% take(9,"hello") = "hello"
-spec take(non_neg_integer(), [T]) -> [T].