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