Created
December 31, 2018 15:30
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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