Created
June 3, 2025 06:47
-
-
Save lagenorhynque/20bcb18a8bac418350930895b2710a9c to your computer and use it in GitHub Desktop.
Factorial and Fibonacci functions in Haskell, Scala, Clojure and Elixir
This file contains hidden or 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
| (ns factorial) | |
| (defn factorial [n] | |
| (if (zero? n) | |
| 1 | |
| (*' n (factorial (dec' n))))) | |
| (defn factorial-seq [] | |
| (reductions *' 1 (iterate inc' 1))) |
This file contains hidden or 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
| defmodule Factorial do | |
| def factorial(0), do: 1 | |
| def factorial(n), do: n * factorial(n - 1) | |
| def factorial_seq do | |
| Stream.concat( | |
| [1], | |
| Stream.scan(Stream.from_index(1), &*/2) | |
| ) | |
| end | |
| end |
This file contains hidden or 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 Factorial where | |
| factorial :: Integral a => a -> a | |
| factorial 0 = 1 | |
| factorial n = n * factorial (n - 1) | |
| factorialSeq :: Integral a => [a] | |
| factorialSeq = scanl (*) 1 [1..] |
This file contains hidden or 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
| object Factorial: | |
| def factorial(n: Int): BigInt = n match | |
| case 0 => 1 | |
| case _ => n * factorial(n - 1) | |
| def factorialSeq: LazyList[BigInt] = | |
| LazyList.from(1).scanLeft(BigInt(1))(_ * _) |
This file contains hidden or 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
| (ns fibonacci) | |
| (defn fib [n] | |
| (case n | |
| 0 0 | |
| 1 1 | |
| (+' (fib (-' n 1)) | |
| (fib (-' n 2))))) | |
| (defn fib-seq [] | |
| (->> [0 1] | |
| (iterate (fn [[a b]] [b (+' a b)])) | |
| (map first))) |
This file contains hidden or 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
| defmodule Fibonacci do | |
| def fib(0), do: 0 | |
| def fib(1), do: 1 | |
| def fib(n), do: fib(n - 1) + fib(n - 2) | |
| def fib_seq do | |
| {0, 1} | |
| |> Stream.iterate(fn {a, b} -> {b, a + b} end) | |
| |> Stream.map(&elem(&1, 0)) | |
| end | |
| end |
This file contains hidden or 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 Fibonacci where | |
| fib :: Integral a => a -> a | |
| fib 0 = 0 | |
| fib 1 = 1 | |
| fib n = fib (n - 1) + fib (n - 2) | |
| fibSeq :: Integral a => [a] | |
| fibSeq = map fst $ iterate (\(a, b) -> (b, a + b)) (0, 1) |
This file contains hidden or 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
| object Fibonacci: | |
| def fib(n: Int): BigInt = n match | |
| case 0 => 0 | |
| case 1 => 1 | |
| case _ => fib(n - 1) + fib(n - 2) | |
| def fibSeq: LazyList[BigInt] = | |
| LazyList.iterate((BigInt(0), BigInt(1))) { case (a, b) => (b, a + b) } | |
| .map(_(0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment