From ad8e62d690d6e33481880a312fa0011883175a20 Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Fri, 31 Jul 2020 06:58:43 +0000 Subject: [PATCH 1/7] Add purescript solution for chapt 1 --- .../Purescript/BinarySearch.purs | 39 ++++++ .../Purescript/README.md | 3 + .../Purescript/packages.dhall | 128 ++++++++++++++++++ .../Purescript/spago.dhall | 9 ++ 4 files changed, 179 insertions(+) create mode 100644 01_introduction_to_algorithms/Purescript/BinarySearch.purs create mode 100644 01_introduction_to_algorithms/Purescript/README.md create mode 100644 01_introduction_to_algorithms/Purescript/packages.dhall create mode 100644 01_introduction_to_algorithms/Purescript/spago.dhall 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..88bf8b6 --- /dev/null +++ b/01_introduction_to_algorithms/Purescript/README.md @@ -0,0 +1,3 @@ +# 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 GrokkingAlgos.SelectionSort` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`) \ 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..093fe82 --- /dev/null +++ b/01_introduction_to_algorithms/Purescript/packages.dhall @@ -0,0 +1,128 @@ +{- +Welcome to your new Dhall package-set! + +Below are instructions for how to edit this file for most use +cases, so that you don't need to know Dhall to use it. + +## Warning: Don't Move This Top-Level Comment! + +Due to how `dhall format` currently works, this comment's +instructions cannot appear near corresponding sections below +because `dhall format` will delete the comment. However, +it will not delete a top-level comment like this one. + +## Use Cases + +Most will want to do one or both of these options: +1. Override/Patch a package's dependency +2. Add a package not already in the default package set + +This file will continue to work whether you use one or both options. +Instructions for each option are explained below. + +### Overriding/Patching a package + +Purpose: +- Change a package's dependency to a newer/older release than the + default package set's release +- Use your own modified version of some dependency that may + include new API, changed API, removed API by + using your custom git repo of the library rather than + the package set's repo + +Syntax: +Replace the overrides' "{=}" (an empty record) with the following idea +The "//" or "⫽" means "merge these two records and + when they have the same value, use the one on the right:" +------------------------------- +let override = + { packageName = + upstream.packageName // { updateEntity1 = "new value", updateEntity2 = "new value" } + , packageName = + upstream.packageName // { version = "v4.0.0" } + , packageName = + upstream.packageName // { repo = "https://www.example.com/path/to/new/repo.git" } + } +------------------------------- + +Example: +------------------------------- +let overrides = + { halogen = + upstream.halogen // { version = "master" } + , halogen-vdom = + upstream.halogen-vdom // { version = "v4.0.0" } + } +------------------------------- + +### Additions + +Purpose: +- Add packages that aren't already included in the default package set + +Syntax: +Replace the additions' "{=}" (an empty record) with the following idea: +------------------------------- +let additions = + { package-name = + { dependencies = + [ "dependency1" + , "dependency2" + ] + , repo = + "https://example.com/path/to/git/repo.git" + , version = + "tag ('v4.0.0') or branch ('master')" + } + , package-name = + { dependencies = + [ "dependency1" + , "dependency2" + ] + , repo = + "https://example.com/path/to/git/repo.git" + , version = + "tag ('v4.0.0') or branch ('master')" + } + , etc. + } +------------------------------- + +Example: +------------------------------- +let additions = + { benchotron = + { dependencies = + [ "arrays" + , "exists" + , "profunctor" + , "strings" + , "quickcheck" + , "lcg" + , "transformers" + , "foldable-traversable" + , "exceptions" + , "node-fs" + , "node-buffer" + , "node-readline" + , "datetime" + , "now" + ] + , repo = + "https://github.com/hdgarrood/purescript-benchotron.git" + , version = + "v7.0.0" + } + } +------------------------------- +-} + + +let upstream = + https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200226/packages.dhall sha256:3a52562e05b31a7b51d12d5b228ccbe567c527781a88e9028ab42374ab55c0f1 + +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" ] +} From a59a2b8886476ca291a8ccbc994081e96892f22f Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Fri, 31 Jul 2020 07:02:40 +0000 Subject: [PATCH 2/7] Fix Readme module name --- 01_introduction_to_algorithms/Purescript/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_introduction_to_algorithms/Purescript/README.md b/01_introduction_to_algorithms/Purescript/README.md index 88bf8b6..87f5a91 100644 --- a/01_introduction_to_algorithms/Purescript/README.md +++ b/01_introduction_to_algorithms/Purescript/README.md @@ -1,3 +1,3 @@ # 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 GrokkingAlgos.SelectionSort` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`) \ No newline at end of file +2. Run file through `spago run --watch --main ` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`) \ No newline at end of file From 1a3782d950330ebb7d1ea892d7685df2b4003487 Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Fri, 31 Jul 2020 07:08:50 +0000 Subject: [PATCH 3/7] update readme with link to main repo --- 01_introduction_to_algorithms/Purescript/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/01_introduction_to_algorithms/Purescript/README.md b/01_introduction_to_algorithms/Purescript/README.md index 87f5a91..a447648 100644 --- a/01_introduction_to_algorithms/Purescript/README.md +++ b/01_introduction_to_algorithms/Purescript/README.md @@ -1,3 +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`) \ No newline at end of file +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 From 3f693b8cf2290272096da7b2979b3bbcd671456d Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Fri, 31 Jul 2020 07:10:55 +0000 Subject: [PATCH 4/7] update package set --- 01_introduction_to_algorithms/Purescript/packages.dhall | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_introduction_to_algorithms/Purescript/packages.dhall b/01_introduction_to_algorithms/Purescript/packages.dhall index 093fe82..97dadbd 100644 --- a/01_introduction_to_algorithms/Purescript/packages.dhall +++ b/01_introduction_to_algorithms/Purescript/packages.dhall @@ -119,7 +119,7 @@ let additions = let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200226/packages.dhall sha256:3a52562e05b31a7b51d12d5b228ccbe567c527781a88e9028ab42374ab55c0f1 + https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9 let overrides = {=} From 32fc3cec95ee804685d345a726040c624a2dcb44 Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Fri, 31 Jul 2020 07:25:32 +0000 Subject: [PATCH 5/7] Add chapter 2 selection sort solution --- 02_selection_sort/Purescript/README.md | 6 + .../Purescript/SelectionSort.purs | 46 +++++++ 02_selection_sort/Purescript/packages.dhall | 128 ++++++++++++++++++ 02_selection_sort/Purescript/spago.dhall | 9 ++ 4 files changed, 189 insertions(+) create mode 100644 02_selection_sort/Purescript/README.md create mode 100644 02_selection_sort/Purescript/SelectionSort.purs create mode 100644 02_selection_sort/Purescript/packages.dhall create mode 100644 02_selection_sort/Purescript/spago.dhall 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..d4da68f --- /dev/null +++ b/02_selection_sort/Purescript/SelectionSort.purs @@ -0,0 +1,46 @@ +module GrokkingAlgos.SelectionSort where + +import Prelude + +import Data.Foldable (minimum) +import Data.List (List(..), delete, (:)) +import Data.Maybe (Maybe(..), fromJust) +import Effect (Effect) +import Effect.Class.Console (logShow) +import Partial.Unsafe (unsafePartial) + +-- PARTIAL +selectionsort :: List Int -> List Int +selectionsort Nil = Nil + +selectionsort l = minVal : (selectionsort rest) + where + minVal :: Int + minVal = unsafePartial $ fromJust $ minimum l + + rest :: List Int + rest = delete minVal l + +-- TOTAL +selectionsort' :: List Int -> Maybe (List Int) +selectionsort' Nil = Just $ Nil + +selectionsort' l = do + mval <- minimum l + let + rest = delete mval l + restSort <- selectionsort' rest + pure $ mval : restSort + +-- +main :: Effect Unit +main = do + logShow $ selectionsort $ 1 : 2 : 3 : 5 : 4 : Nil + logShow $ selectionsort' $ 1 : 2 : 3 : 5 : 4 : Nil + + +{- + -- | Notes + -- | Array (Reading (O(1))) (Insertion (O(n))) + -- | Linked List (Reading - O(n)) (Insertion - O(1)) -- Constants in Big O???? + -} \ No newline at end of file diff --git a/02_selection_sort/Purescript/packages.dhall b/02_selection_sort/Purescript/packages.dhall new file mode 100644 index 0000000..97dadbd --- /dev/null +++ b/02_selection_sort/Purescript/packages.dhall @@ -0,0 +1,128 @@ +{- +Welcome to your new Dhall package-set! + +Below are instructions for how to edit this file for most use +cases, so that you don't need to know Dhall to use it. + +## Warning: Don't Move This Top-Level Comment! + +Due to how `dhall format` currently works, this comment's +instructions cannot appear near corresponding sections below +because `dhall format` will delete the comment. However, +it will not delete a top-level comment like this one. + +## Use Cases + +Most will want to do one or both of these options: +1. Override/Patch a package's dependency +2. Add a package not already in the default package set + +This file will continue to work whether you use one or both options. +Instructions for each option are explained below. + +### Overriding/Patching a package + +Purpose: +- Change a package's dependency to a newer/older release than the + default package set's release +- Use your own modified version of some dependency that may + include new API, changed API, removed API by + using your custom git repo of the library rather than + the package set's repo + +Syntax: +Replace the overrides' "{=}" (an empty record) with the following idea +The "//" or "⫽" means "merge these two records and + when they have the same value, use the one on the right:" +------------------------------- +let override = + { packageName = + upstream.packageName // { updateEntity1 = "new value", updateEntity2 = "new value" } + , packageName = + upstream.packageName // { version = "v4.0.0" } + , packageName = + upstream.packageName // { repo = "https://www.example.com/path/to/new/repo.git" } + } +------------------------------- + +Example: +------------------------------- +let overrides = + { halogen = + upstream.halogen // { version = "master" } + , halogen-vdom = + upstream.halogen-vdom // { version = "v4.0.0" } + } +------------------------------- + +### Additions + +Purpose: +- Add packages that aren't already included in the default package set + +Syntax: +Replace the additions' "{=}" (an empty record) with the following idea: +------------------------------- +let additions = + { package-name = + { dependencies = + [ "dependency1" + , "dependency2" + ] + , repo = + "https://example.com/path/to/git/repo.git" + , version = + "tag ('v4.0.0') or branch ('master')" + } + , package-name = + { dependencies = + [ "dependency1" + , "dependency2" + ] + , repo = + "https://example.com/path/to/git/repo.git" + , version = + "tag ('v4.0.0') or branch ('master')" + } + , etc. + } +------------------------------- + +Example: +------------------------------- +let additions = + { benchotron = + { dependencies = + [ "arrays" + , "exists" + , "profunctor" + , "strings" + , "quickcheck" + , "lcg" + , "transformers" + , "foldable-traversable" + , "exceptions" + , "node-fs" + , "node-buffer" + , "node-readline" + , "datetime" + , "now" + ] + , repo = + "https://github.com/hdgarrood/purescript-benchotron.git" + , version = + "v7.0.0" + } + } +------------------------------- +-} + + +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" ] +} From 23f31b4f3f48f9ab669e369ed3addc909c6daca9 Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Tue, 4 Aug 2020 11:32:03 +0000 Subject: [PATCH 6/7] Cleanup packages dhall and remove notes --- .../Purescript/packages.dhall | 120 ------------------ .../Purescript/SelectionSort.purs | 7 - 02_selection_sort/Purescript/packages.dhall | 120 ------------------ 3 files changed, 247 deletions(-) diff --git a/01_introduction_to_algorithms/Purescript/packages.dhall b/01_introduction_to_algorithms/Purescript/packages.dhall index 97dadbd..3ecb3c9 100644 --- a/01_introduction_to_algorithms/Purescript/packages.dhall +++ b/01_introduction_to_algorithms/Purescript/packages.dhall @@ -1,123 +1,3 @@ -{- -Welcome to your new Dhall package-set! - -Below are instructions for how to edit this file for most use -cases, so that you don't need to know Dhall to use it. - -## Warning: Don't Move This Top-Level Comment! - -Due to how `dhall format` currently works, this comment's -instructions cannot appear near corresponding sections below -because `dhall format` will delete the comment. However, -it will not delete a top-level comment like this one. - -## Use Cases - -Most will want to do one or both of these options: -1. Override/Patch a package's dependency -2. Add a package not already in the default package set - -This file will continue to work whether you use one or both options. -Instructions for each option are explained below. - -### Overriding/Patching a package - -Purpose: -- Change a package's dependency to a newer/older release than the - default package set's release -- Use your own modified version of some dependency that may - include new API, changed API, removed API by - using your custom git repo of the library rather than - the package set's repo - -Syntax: -Replace the overrides' "{=}" (an empty record) with the following idea -The "//" or "⫽" means "merge these two records and - when they have the same value, use the one on the right:" -------------------------------- -let override = - { packageName = - upstream.packageName // { updateEntity1 = "new value", updateEntity2 = "new value" } - , packageName = - upstream.packageName // { version = "v4.0.0" } - , packageName = - upstream.packageName // { repo = "https://www.example.com/path/to/new/repo.git" } - } -------------------------------- - -Example: -------------------------------- -let overrides = - { halogen = - upstream.halogen // { version = "master" } - , halogen-vdom = - upstream.halogen-vdom // { version = "v4.0.0" } - } -------------------------------- - -### Additions - -Purpose: -- Add packages that aren't already included in the default package set - -Syntax: -Replace the additions' "{=}" (an empty record) with the following idea: -------------------------------- -let additions = - { package-name = - { dependencies = - [ "dependency1" - , "dependency2" - ] - , repo = - "https://example.com/path/to/git/repo.git" - , version = - "tag ('v4.0.0') or branch ('master')" - } - , package-name = - { dependencies = - [ "dependency1" - , "dependency2" - ] - , repo = - "https://example.com/path/to/git/repo.git" - , version = - "tag ('v4.0.0') or branch ('master')" - } - , etc. - } -------------------------------- - -Example: -------------------------------- -let additions = - { benchotron = - { dependencies = - [ "arrays" - , "exists" - , "profunctor" - , "strings" - , "quickcheck" - , "lcg" - , "transformers" - , "foldable-traversable" - , "exceptions" - , "node-fs" - , "node-buffer" - , "node-readline" - , "datetime" - , "now" - ] - , repo = - "https://github.com/hdgarrood/purescript-benchotron.git" - , version = - "v7.0.0" - } - } -------------------------------- --} - - let upstream = https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9 diff --git a/02_selection_sort/Purescript/SelectionSort.purs b/02_selection_sort/Purescript/SelectionSort.purs index d4da68f..435fd69 100644 --- a/02_selection_sort/Purescript/SelectionSort.purs +++ b/02_selection_sort/Purescript/SelectionSort.purs @@ -37,10 +37,3 @@ main :: Effect Unit main = do logShow $ selectionsort $ 1 : 2 : 3 : 5 : 4 : Nil logShow $ selectionsort' $ 1 : 2 : 3 : 5 : 4 : Nil - - -{- - -- | Notes - -- | Array (Reading (O(1))) (Insertion (O(n))) - -- | Linked List (Reading - O(n)) (Insertion - O(1)) -- Constants in Big O???? - -} \ No newline at end of file diff --git a/02_selection_sort/Purescript/packages.dhall b/02_selection_sort/Purescript/packages.dhall index 97dadbd..3ecb3c9 100644 --- a/02_selection_sort/Purescript/packages.dhall +++ b/02_selection_sort/Purescript/packages.dhall @@ -1,123 +1,3 @@ -{- -Welcome to your new Dhall package-set! - -Below are instructions for how to edit this file for most use -cases, so that you don't need to know Dhall to use it. - -## Warning: Don't Move This Top-Level Comment! - -Due to how `dhall format` currently works, this comment's -instructions cannot appear near corresponding sections below -because `dhall format` will delete the comment. However, -it will not delete a top-level comment like this one. - -## Use Cases - -Most will want to do one or both of these options: -1. Override/Patch a package's dependency -2. Add a package not already in the default package set - -This file will continue to work whether you use one or both options. -Instructions for each option are explained below. - -### Overriding/Patching a package - -Purpose: -- Change a package's dependency to a newer/older release than the - default package set's release -- Use your own modified version of some dependency that may - include new API, changed API, removed API by - using your custom git repo of the library rather than - the package set's repo - -Syntax: -Replace the overrides' "{=}" (an empty record) with the following idea -The "//" or "⫽" means "merge these two records and - when they have the same value, use the one on the right:" -------------------------------- -let override = - { packageName = - upstream.packageName // { updateEntity1 = "new value", updateEntity2 = "new value" } - , packageName = - upstream.packageName // { version = "v4.0.0" } - , packageName = - upstream.packageName // { repo = "https://www.example.com/path/to/new/repo.git" } - } -------------------------------- - -Example: -------------------------------- -let overrides = - { halogen = - upstream.halogen // { version = "master" } - , halogen-vdom = - upstream.halogen-vdom // { version = "v4.0.0" } - } -------------------------------- - -### Additions - -Purpose: -- Add packages that aren't already included in the default package set - -Syntax: -Replace the additions' "{=}" (an empty record) with the following idea: -------------------------------- -let additions = - { package-name = - { dependencies = - [ "dependency1" - , "dependency2" - ] - , repo = - "https://example.com/path/to/git/repo.git" - , version = - "tag ('v4.0.0') or branch ('master')" - } - , package-name = - { dependencies = - [ "dependency1" - , "dependency2" - ] - , repo = - "https://example.com/path/to/git/repo.git" - , version = - "tag ('v4.0.0') or branch ('master')" - } - , etc. - } -------------------------------- - -Example: -------------------------------- -let additions = - { benchotron = - { dependencies = - [ "arrays" - , "exists" - , "profunctor" - , "strings" - , "quickcheck" - , "lcg" - , "transformers" - , "foldable-traversable" - , "exceptions" - , "node-fs" - , "node-buffer" - , "node-readline" - , "datetime" - , "now" - ] - , repo = - "https://github.com/hdgarrood/purescript-benchotron.git" - , version = - "v7.0.0" - } - } -------------------------------- --} - - let upstream = https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9 From 2f166a0ee16cc8cc6a82df03f8648204af67e473 Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Wed, 5 Aug 2020 12:58:43 +0000 Subject: [PATCH 7/7] Update selection sort --- .../Purescript/SelectionSort.purs | 30 +++---------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/02_selection_sort/Purescript/SelectionSort.purs b/02_selection_sort/Purescript/SelectionSort.purs index 435fd69..6b55aff 100644 --- a/02_selection_sort/Purescript/SelectionSort.purs +++ b/02_selection_sort/Purescript/SelectionSort.purs @@ -1,39 +1,17 @@ module GrokkingAlgos.SelectionSort where import Prelude - import Data.Foldable (minimum) import Data.List (List(..), delete, (:)) -import Data.Maybe (Maybe(..), fromJust) +import Data.Maybe (Maybe(..)) import Effect (Effect) import Effect.Class.Console (logShow) -import Partial.Unsafe (unsafePartial) --- PARTIAL selectionsort :: List Int -> List Int -selectionsort Nil = Nil +selectionsort l = case minimum l of + Nothing -> Nil + Just min -> min : selectionsort (delete min l) -selectionsort l = minVal : (selectionsort rest) - where - minVal :: Int - minVal = unsafePartial $ fromJust $ minimum l - - rest :: List Int - rest = delete minVal l - --- TOTAL -selectionsort' :: List Int -> Maybe (List Int) -selectionsort' Nil = Just $ Nil - -selectionsort' l = do - mval <- minimum l - let - rest = delete mval l - restSort <- selectionsort' rest - pure $ mval : restSort - --- main :: Effect Unit main = do logShow $ selectionsort $ 1 : 2 : 3 : 5 : 4 : Nil - logShow $ selectionsort' $ 1 : 2 : 3 : 5 : 4 : Nil