Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created December 31, 2018 15:30
Show Gist options
  • Save DonaldKellett/d5b4615173b1e1cf3c5a2a9394f1aed5 to your computer and use it in GitHub Desktop.
Save DonaldKellett/d5b4615173b1e1cf3c5a2a9394f1aed5 to your computer and use it in GitHub Desktop.
PureScript by Example - 4. Recursion, Maps And Folds - 4.4 Recursion on Arrays - Solutions to Exercises 1-2
module Recursion where
-- 4.4 Recursion on Arrays of PureScript by Example
import Prelude
import Data.Array.Partial
import Partial.Unsafe
-- Exercise 1
isEven :: Int -> Boolean
isEven 0 = true
isEven 1 = false
isEven n = isEven (n - 2)
-- Exercise 2
numEven :: Array Int -> Int
numEven [] = 0
numEven arr =
(if isEven (unsafePartial head arr) then 1 else 0) +
numEven (unsafePartial tail arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment