K

@kenbitanga

Haskell: Fizzbuzz

Python
2 years ago
fizzbuzz n | mod n 15 == 0 = "FizzBuzz" | mod n 3 == 0 = "Fizz" | mod n 5 == 0 = "Buzz" | otherwise = show n main = do let fbuzz n = map fizzbuzz [1..n] mapM_ print $ fbuzz 15

Python: Factorial Performance - For Loop - While Loop - Naive Recursion - Tail Recursion

Python
2 years ago
import time def fac_naive_recursive(n): if n <= 1: return 1 return n * fac_naive_recursive(n-1) def fac_tail_recursive(n, a=1): if n < 1: return a return fac_tail_recursive(n-1, a*n)

Python: Factorial - For Loop - While Loop - Naive Recursion - Tail Recursion

Python
2 years ago
def fac_naive_recursive(n): if n <= 1: return 1 return n * fac_naive_recursive(n-1) def fac_tail_recursive(n, a=1): if n < 1: return a return fac_tail_recursive(n-1, a*n) def fac_for_loop(n, a=1): for i in range(1, n+1):

Python: Fibonacci - Iterative vs Generative vs Naive Recursive vs Tail Recursive

Python
2 years ago
def fib_for_loop(n, a=0, b=1): for _ in range(n): a, b = b, a+b return a def fib_while_loop(n, a=0, b=1): while True: if n < 1: return a a, b = b, a+b n = n-1

FSharp - Filter Function in Various Ways

Python
2 years ago
let rec filt f li = match li with | [] -> [] | x::xs -> match f x with | true -> x :: filt f xs | false -> filt f xs let rec filt f = function | [] -

Haskell vs FSharp vs OCaml - Filter Function

Python
2 years ago
# Haskell Filter Function filt _ [] = [] filt f (x:xs) | f x = x : filt f xs | otherwise = filt f xs main = do let nums = [1..10] let odds = filt odd [1..10] print $ nums

Reducer Function in Haskel, OCaml and FSharp

Python
2 years ago
# haskell reduce f ac [] = ac reduce f ac (x:xs) = reduce f (f ac x) xs main = do putStrLn "Hello, World!" let prod = reduce (*) 1 [1..5] print $ prod # ocaml

Haskell: Implement Each, Map, Filter, Reduce Functions

Python
2 years ago
each f [] = return () each f (x:xs) = do f x each f xs mapp f [] = [] mapp f (x:xs) = f x : mapp f xs filt f [] = [] filt f (x:xs)

FSharp: Implement Reduce Function

Python
2 years ago
let rec reduce fn acc lst = match lst with | [] -> acc | x::xs -> let accu = fn acc x reduce fn accu xs [<EntryPoint>] let main argv = let nums = [1..5]

Elixir: Implement Enum.map/2

Python
2 years ago
defmodule Enu do def map([], _), do: [] def map([x | xs], fn) do [fn.(x) | map(xs, fn)] end end # usage Enu.map([1,2,3], fn x -> x * 2 end)

Haskell: Implement Reducer Function

Python
2 years ago
reduce fn acc [] = acc reduce fn acc (x:xs) = do let accu = fn acc x reduce fn accu xs main = do let nums = [1..5] print $ nums print $ reduce (+) 0 nums print $ reduce (*) 1 nums

Haskell: List Comprehension

Python
2 years ago
[n*2 | n <- [1..10]] [n*2 | n <- [1..10], odd n]

Haskell: Each / ForEach Function

Python
2 years ago
each :: [a] -> (a -> IO ()) -> IO () each [] _ = return () each (hd:tl) fn = do fn hd each tl fn

Python: Implement Inclusive Range Function With Step Parameter Recursively

Python
2 years ago
def ranger(lo, hi, step=1): if (step < 1) or (lo > hi): return [] return [lo, *ranger(lo + step, hi, step)] print(ranger(0, 10, 2))

Python: Implement Each, Map, Filter & Reduce Recursively

Python
2 years ago
def each(li, fn): if len(li) == 0: return head, *tail = li fn(head) return each(tail, fn) def map(li, fn): if len(li) == 0: return [] head, *tail = li return [fn(head)] + map(tail, fn)

Python: Iterative vs Recursive Reducer Function

Python
2 years ago
# iterative left & right reducer functions def iReduce(li, fn, acc=0): for x in li: acc = fn(acc, x) return acc def iReduceRight(li, fn, acc=0): for x in reversed(li): acc = fn(acc, x)

Ruby: Recursive Filter Method

Ruby
2 years ago
def rFilter(fn, arr) return [] if arr.empty? head, *tail = arr if fn.call(head) return [head] + rFilter(fn, tail) end rFilter(fn, tail) end numbers = [1, 2, 3, 4, 5]

Haskell: Recursive Filter Implementation

Python
2 years ago
# Haskell Filter Implementation myFilter :: (a -> Bool) -> [a] -> [a] myFilter fn [] = [] myFilter fn (x:xs) | fn x = x : myFilter fn xs | otherwise = myFilter fn xs main = do putStrLn "Filter implementation"

JS: Recursive Array Filter Function

NodeJS
2 years ago
function rFilter(fn, arr) { if (arr.length === 0) return []; [head, ...tail] = arr; return fn(head) ? [head].concat(rFilter(fn, tail)) : rFilter(fn, tail); } arr = [1,2,3,4,5]; console.log('input array:', arr);

Python: Implement Filter List Function Using Iteration and Recursion

Python
2 years ago
def iFilter(fn, li): result = [] for x in li: if fn(x): result.append(x) return result def rFilter(fn, li): if len(li) == 0: return [] head, *tail = li