diff --git a/01_introduction_to_algorithms/Purescript/BinarySearch.purs b/01_introduction_to_algorithms/Purescript/BinarySearch.purs new file mode 100644 index 0000000..81ed644 --- /dev/null +++ b/01_introduction_to_algorithms/Purescript/BinarySearch.purs @@ -0,0 +1,39 @@ +module GrokkingAlgos.BinarySearch where + +import Prelude +import Data.Array ((!!)) +import Data.Array as Array +import Data.Maybe (Maybe(..)) +import Effect (Effect) +import Effect.Class.Console (logShow) + +-- | Binary Search - input is a sorted list of elements +-- | Big o notation - log n +-- | Traveling salesman - O (n!) +binarysearch :: Int -> Array Int -> Int -> Int -> Maybe Int +binarysearch x arr low high + | low > high = Nothing + | otherwise = + let + mid = (high + low) / 2 + in + arr !! mid + >>= case _ of + item + | item == x -> Just mid + item + | item > x -> binarysearch x arr low (mid - 1) + item + | item < x -> binarysearch x arr (mid + 1) high + _ -> Nothing + +find :: Int -> Array Int -> Maybe Int +find x arr = binarysearch x arr low high + where + low = 0 + + high = (Array.length arr) - 1 + +main :: Effect Unit +main = do + logShow $ find 20 [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20 ] diff --git a/01_introduction_to_algorithms/Purescript/README.md b/01_introduction_to_algorithms/Purescript/README.md new file mode 100644 index 0000000..a447648 --- /dev/null +++ b/01_introduction_to_algorithms/Purescript/README.md @@ -0,0 +1,6 @@ +# Getting Started +1. Install `spago` and `purescript` through `yarn` or `npm` (e.g. `yarn global add spago` and `yarn global add purescript`) +2. Run file through `spago run --watch --main ` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`) + +# Main Repo +- https://github.com/seanyu4296/grokking-algo-in-purs \ No newline at end of file diff --git a/01_introduction_to_algorithms/Purescript/packages.dhall b/01_introduction_to_algorithms/Purescript/packages.dhall new file mode 100644 index 0000000..3ecb3c9 --- /dev/null +++ b/01_introduction_to_algorithms/Purescript/packages.dhall @@ -0,0 +1,8 @@ +let upstream = + https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9 + +let overrides = {=} + +let additions = {=} + +in upstream // overrides // additions diff --git a/01_introduction_to_algorithms/Purescript/spago.dhall b/01_introduction_to_algorithms/Purescript/spago.dhall new file mode 100644 index 0000000..721cd6d --- /dev/null +++ b/01_introduction_to_algorithms/Purescript/spago.dhall @@ -0,0 +1,9 @@ +{- +Welcome to a Spago project! +You can edit this file as you like. +-} +{ name = "my-project" +, dependencies = [ "console", "effect", "psci-support", "arrays", "debug", "lists", "ordered-collections", "strings"] +, packages = ./packages.dhall +, sources = [ "src/**/*.purs", "test/**/*.purs" ] +} diff --git a/02_selection_sort/Purescript/README.md b/02_selection_sort/Purescript/README.md new file mode 100644 index 0000000..a447648 --- /dev/null +++ b/02_selection_sort/Purescript/README.md @@ -0,0 +1,6 @@ +# Getting Started +1. Install `spago` and `purescript` through `yarn` or `npm` (e.g. `yarn global add spago` and `yarn global add purescript`) +2. Run file through `spago run --watch --main ` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`) + +# Main Repo +- https://github.com/seanyu4296/grokking-algo-in-purs \ No newline at end of file diff --git a/02_selection_sort/Purescript/SelectionSort.purs b/02_selection_sort/Purescript/SelectionSort.purs new file mode 100644 index 0000000..6b55aff --- /dev/null +++ b/02_selection_sort/Purescript/SelectionSort.purs @@ -0,0 +1,17 @@ +module GrokkingAlgos.SelectionSort where + +import Prelude +import Data.Foldable (minimum) +import Data.List (List(..), delete, (:)) +import Data.Maybe (Maybe(..)) +import Effect (Effect) +import Effect.Class.Console (logShow) + +selectionsort :: List Int -> List Int +selectionsort l = case minimum l of + Nothing -> Nil + Just min -> min : selectionsort (delete min l) + +main :: Effect Unit +main = do + logShow $ selectionsort $ 1 : 2 : 3 : 5 : 4 : Nil diff --git a/02_selection_sort/Purescript/packages.dhall b/02_selection_sort/Purescript/packages.dhall new file mode 100644 index 0000000..3ecb3c9 --- /dev/null +++ b/02_selection_sort/Purescript/packages.dhall @@ -0,0 +1,8 @@ +let upstream = + https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9 + +let overrides = {=} + +let additions = {=} + +in upstream // overrides // additions diff --git a/02_selection_sort/Purescript/spago.dhall b/02_selection_sort/Purescript/spago.dhall new file mode 100644 index 0000000..721cd6d --- /dev/null +++ b/02_selection_sort/Purescript/spago.dhall @@ -0,0 +1,9 @@ +{- +Welcome to a Spago project! +You can edit this file as you like. +-} +{ name = "my-project" +, dependencies = [ "console", "effect", "psci-support", "arrays", "debug", "lists", "ordered-collections", "strings"] +, packages = ./packages.dhall +, sources = [ "src/**/*.purs", "test/**/*.purs" ] +}