From 2f166a0ee16cc8cc6a82df03f8648204af67e473 Mon Sep 17 00:00:00 2001 From: seanyu4296 Date: Wed, 5 Aug 2020 12:58:43 +0000 Subject: [PATCH] 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