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
% flatten/2 - Checks whether the second argument is a fully flattened form of the first, both of which are lists | |
% Caveat - This solution doesn't make use of append/3 as the exercise page explicitly stated but uses append/2 instead (probably not what the author intended) :p | |
flatten([], []). | |
flatten([X | List], [X | Flat]) :- \+ is_list(X), flatten(List, Flat). | |
flatten([X | List], NewFlat) :- is_list(X), flatten(X, XFlat), flatten(List, Flat), append([XFlat, Flat], NewFlat). |
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 CircleArea where | |
import Prelude | |
import Math | |
circleArea :: Number -> Number | |
circleArea r = pi * r * r |
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 AddressBook where | |
-- Boilerplate code given in "3. Functions and Records" of "PureScript by Example" | |
import Prelude | |
import Control.Plus (empty) | |
import Data.List (List(..), filter, head, nubBy) | |
import Data.Maybe (Maybe, isJust) |
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 |
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 MapAndFilter where | |
-- 4.7 Filtering Arrays of PureScript by Example | |
import Prelude | |
import Data.Array | |
-- Exercise 1 | |
squared :: Array Number -> Array Number | |
squared = map (\n -> n * n) |
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 Guards where | |
-- 4.11 Guards of PureScript by Example | |
-- Boilerplate code provided by PureScript by Example | |
import Prelude | |
import Data.Array | |
import Control.MonadZero (guard) |
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 Folds where | |
import Prelude | |
import Data.Foldable | |
import Data.Array.Partial | |
import Partial.Unsafe | |
-- Exercise 1 | |
allTrue :: Array Boolean -> Boolean | |
allTrue = foldl (&&) true |
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 FileOperations where | |
-- 4.17 Listing All Files of PureScript by Example | |
-- N.B. This module relies extensively on a non-standard module Data.Path | |
-- whose source code can be found on | |
-- https://github.com/paf31/purescript-book/blob/master/exercises/chapter4/src/Data/Path.purs | |
import Prelude | |
import Data.Array | |
import Partial.Unsafe |
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 Guards where | |
import Prelude | |
-- Exercise 1 | |
factorial :: Int -> Int | |
factorial 0 = 1 | |
factorial n = n * factorial (n - 1) | |
-- Exercise 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 NamedPatterns where | |
import Prelude | |
-- Boilerplate code provided in 5.8 Nested Patterns of PureScript by Example | |
type Address = { street :: String, city :: String } | |
type Person = { name :: String, address :: Address } | |
-- Exercise 1 | |
sameCity :: Person -> Person -> Boolean |