Merge pull request #172 from seanyu4296/add-purescript-solution-for-chapter-1-chapter-2

Add purescript solution for chapter 1 chapter 2
This commit is contained in:
Aditya Bhargava
2020-09-14 12:52:10 -05:00
committed by GitHub
8 changed files with 102 additions and 0 deletions

View File

@@ -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 ]

View File

@@ -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 <module-name>` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`)
# Main Repo
- https://github.com/seanyu4296/grokking-algo-in-purs

View File

@@ -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

View File

@@ -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" ]
}

View File

@@ -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 <module-name>` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`)
# Main Repo
- https://github.com/seanyu4296/grokking-algo-in-purs

View File

@@ -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

View File

@@ -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

View File

@@ -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" ]
}