From 6ae8d0b8e4e114492c1192c6b5df0695c1ef5c04 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 1 Feb 2024 18:44:01 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 1 + fib.roc | 28 ++++++++++++++++++++++++ higher-order.roc | 17 +++++++++++++++ prime.roc | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 .gitignore create mode 100644 fib.roc create mode 100644 higher-order.roc create mode 100644 prime.roc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fcb152 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out diff --git a/fib.roc b/fib.roc new file mode 100644 index 0000000..b5b8192 --- /dev/null +++ b/fib.roc @@ -0,0 +1,28 @@ +app "fibonacci" + packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.1/Icc3xJoIixF3hCcfXrDwLCu4wQHtNdPyoJkEbkgIElA.tar.br" } + imports [pf.Stdout, pf.Task] + provides [main] to pf + +main : Task.Task {} I32 +main = + numStrings = + List.range {start: At 0u128, end: At 186u128} # 186 is all we can do with 128 bit + |> List.walk [] fibWalk + |> List.map Num.toStr + |> Str.joinWith "\n" + + Stdout.line "$(numStrings)" + +fibWalk : List (Num a), Num a -> List (Num a) +fibWalk = \l, n -> + next = when l is + [] | [_] -> fib n + [.., a, b] -> a + b + List.append l next + +fib : Num a -> Num a +fib = \n -> + if n < 2 then + n + else + fib (n - 1) + fib (n - 2) diff --git a/higher-order.roc b/higher-order.roc new file mode 100644 index 0000000..f69ecd8 --- /dev/null +++ b/higher-order.roc @@ -0,0 +1,17 @@ +app "higher-order" + packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.1/Icc3xJoIixF3hCcfXrDwLCu4wQHtNdPyoJkEbkgIElA.tar.br" } + imports [pf.Stdout, pf.Task] + provides [main] to pf + +main : Task.Task {} I32 +main = + mySum = bind anySum 2 + Stdout.line "$(Num.toStr (mySum 2))" + +bind : (a, b -> c), a -> (b -> c) +bind = \fn, a -> + \b -> fn a b + +anySum : Num a, Num a -> Num a +anySum = \a, b -> + a + b + 2 diff --git a/prime.roc b/prime.roc new file mode 100644 index 0000000..6da8b91 --- /dev/null +++ b/prime.roc @@ -0,0 +1,56 @@ +app "prime" + packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.1/Icc3xJoIixF3hCcfXrDwLCu4wQHtNdPyoJkEbkgIElA.tar.br" } + imports [pf.Stdout, pf.Task] + provides [main] to pf + +main : Task.Task {} I32 +main = + numStrings = + List.range {start: At 0u64, end: At 1_000_000u64} + |> List.map \e -> isPrimePre e + |> List.walk [] primeWalk + |> List.map Num.toStr + |> Str.joinWith "\n" + + Stdout.line "$(numStrings)" + +PrimeResult : [No U64, Yes U64, Maybe U64] + +primeWalk : List U64, PrimeResult -> List U64 +primeWalk = \l, n -> + when n is + Yes n2 -> + List.append l n2 + Maybe n2 -> + if isPrimePost l n2 then + List.append l n2 + else + l + No _ -> l + +isPrimePre : U64 -> PrimeResult +isPrimePre = \n -> + if n < 2 then + No n + else if n < 4 then + Yes n + else if ( firstPrimes |> List.any \p -> n > p && n % p == 0 ) then + No n + else + Maybe n + +isPrimePost : List U64, U64 -> Bool +isPrimePost = \l, n -> + nFloat = (Num.toF64 n) + valLimit = Num.ceiling (Num.sqrt nFloat) + # approximate the number of primes < sqrt(n) to limit the range we have to check with a 25% error tolerance + valLimitF = Num.toF64 valLimit + limit = Num.ceiling (valLimitF * 1.25 / (Num.log valLimitF)) + l + |> List.sublist {start: 0, len: limit} + |> List.dropIf \p -> p > valLimit + |> List.any \p -> n % p == 0 + |> Bool.not + +firstPrimes = + [2, 3, 5, 7, 9, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 51, 67, 71, 73, 79, 83, 89, 97]