Sergej Chodarev (sergejx.net)
function factorial(n) {
var fact = 1;
for (var i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
function factorial(n) {
return (n == 1) ? 1
: n * factorial(n - 1);
}
Funkcia, ktorá zistí, či všetky slova v zozname majú nepárny počet znakov.
function allOdd(words) {
var result = true;
for (var i = 0; i < words.length; i++) {
var len = words[i].length;
if (!odd(len)) {
result = false;
break;
}
}
return result;
}
function allOdd(words) {
return reduce(and, map(compose(odd, length), words));
}
Funkcia, ktorej argumentom alebo výsledkom je funkcia.
boolean allOdd(List<String> words) {
Function<Integer, Boolean> odd = (n) -> n % 2 == 1;
return words.stream().map(odd.compose(String::length))
.reduce(Boolean::logicalAnd)
.get();
}
allOdd :: [String] -> Bool
allOdd words = and (map (odd . length) words)
square x = x * x
add a b = a + b
plusThree x = add x 3
max x y | x >= y = x
| otherwise = y
-- alebo
max x y
| x >= y = x
| otherwise = y
factorial n
| n == 1 = 1
| n >= 1 = n * factorial (n-1)
factorial :: Int -> Int
factorial n
| n == 1 = 1
| n >= 1 = n * factorial (n-1)
max :: Int -> Int -> Int
max a b | a >= b = a
| otherwise = b
(1, "ahoj", True)
fst (x, y) = x -- pattern matching
snd (x, y) = y
aList = [1, 1, 2, 3, 5, 8, 13, 21]
sum [] = 0
sum (x:xs) = x + sum xs
quad a b c | discr == 0 = [-b/(2*a)]
| discr > 0 = [-(b + sqrt discr) / (2*a),
-(b - sqrt discr) / (2*a)]
where discr = b^2 - 4*a*c