This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(first). | |
-export([double/1, mult/2, area/3, square/1, treble/1]). | |
mult(X,Y) -> | |
X*Y. | |
double(X) -> | |
mult(2,X). | |
area(A,B,C) -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(patterns). | |
-export([is_zero/1, xOr/2, xOr2/2, xOr3/2, xOr4/2, xOr5/2]). | |
-export([maxThree/3, howManyEqual/3]). | |
% | |
% The following functions are the examples provided in step 1.14 | |
% | |
is_zero(0) -> | |
true; | |
is_zero(_) -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(recursion). | |
-export([fib/1, pieces/1, cake/2, factorial/1, binomialcoefficient/2]). | |
% Give a recursive definition of the function fib/1 computing the | |
% Fibonacci numbers... | |
fib(0) -> 0; | |
fib(1) -> 1; | |
fib(X) -> (fib (X-1)) + (fib (X-2)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(recursion2). | |
-export([fib/1, fib/3, fib_tests/0, perfect_tests/0, perfect/1]). | |
% An efficient implementation of fibonacci | |
fib(N) -> fib(N, 0, 1). | |
fib(0, Prev, _) -> Prev; | |
fib(Cnt, Prev, Curr) -> fib(Cnt-1, Curr, Prev+Curr). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(listfuncs). | |
-export([productd/1, productt/1, productt/2]). | |
-export([listmaxd/1, listmaxt/1, listmaxt/2]). | |
% | |
% Combining list elements: the product of a list | |
% | |
% Direct Recursion | |
productd([]) -> 1; % The multiplying by 0 gives 0, so need to return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(listcons). | |
-export([double/1]). | |
-export([doubleelems/1]). | |
-export([even/1,evens/1]). | |
% Erlang Course activity 2.9 | |
% Transforming List Elements | |
% Define an Erlang function double/1 to double the elements of a list of numbers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(take). | |
-export([take/2, take_test/0]). | |
-spec take(integer(), [T]) -> [T]. | |
take(0, _) -> []; | |
take(_, []) -> []; | |
take(Cnt, [X|Xs]) when Cnt > 0 -> [X | take(Cnt-1, Xs)]. | |
take_test() -> | |
[] = take(0,"hello"), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(dedupe). | |
-export([exists/2,nubfb/1,nubfb/2,nubbf/1,nubbf/2,nub_test/0]). | |
% nubfb([2,4,1,3,3,1]) = [2,4,1,3] | |
% nubbf([2,4,1,3,3,1]) = [2,4,3,1] | |
exists(_, []) -> false; | |
exists(Val, [X|_Xs]) when Val == X -> true; | |
exists(Val, [X|Xs]) when Val =/= X -> exists(Val, Xs). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(dedupe2). | |
-export([exists/2,nubfb/1,nubfb/2,nubbf/1,nubbf/2,nub_test/0]). | |
% nubfb([2,4,1,3,3,1]) = [2,4,1,3] | |
% nubbf([2,4,1,3,3,1]) = [2,4,3,1] | |
exists(Val, []) -> [Val]; | |
exists(Val, [X|_Xs]) when Val == X -> []; | |
exists(Val, [X|Xs]) when Val =/= X -> exists(Val, Xs). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(palindrome). | |
-export([palindrome/1, clean/1, clean/2, palindrome_test/0]). | |
% palindrome("Madam I\'m Adam") = true | |
palindrome([]) -> []; | |
palindrome(List) -> | |
CleanList = clean(List), | |
ReversedList = lists:reverse(CleanList), |
OlderNewer