Skip to content

Instantly share code, notes, and snippets.

View DonaldKellett's full-sized avatar

Donald Sebastian Leung DonaldKellett

View GitHub Profile
@DonaldKellett
DonaldKellett / lpn-6-practical-3.pl
Created December 23, 2018 15:35
Learn Prolog Now! - Chapter 6 - Practical Session - Programming Exercise 3 - Flatten a list
% 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).
@DonaldKellett
DonaldKellett / CircleArea.purs
Created December 31, 2018 10:26
PureScript by Example - 2.11 Testing Code Using the Interactive Mode - Exercise 1 Solution
module CircleArea where
import Prelude
import Math
circleArea :: Number -> Number
circleArea r = pi * r * r
@DonaldKellett
DonaldKellett / AddressBook.purs
Created December 31, 2018 14:53
PureScript By Example - 3. Functions and Records - 3.15 Tests, Tests, Tests ... - Solutions to Exercises 2-4
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)
@DonaldKellett
DonaldKellett / Recursion.purs
Created December 31, 2018 15:30
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
@DonaldKellett
DonaldKellett / MapAndFilter.purs
Created December 31, 2018 16:06
PureScript by Example - 4.7 Filtering Arrays - Exercise 1-3 Solutions
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)
@DonaldKellett
DonaldKellett / Guards.purs
Created January 1, 2019 08:51
PureScript by Example - 4.11 Guards - Exercise 1-4 Solutions
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)
@DonaldKellett
DonaldKellett / Folds.purs
Created January 1, 2019 10:15
PureScript by Example - 4.15 Prefer Folds to Explicit Recursion - Exercise 1, 3, 4 Solutions
module Folds where
import Prelude
import Data.Foldable
import Data.Array.Partial
import Partial.Unsafe
-- Exercise 1
allTrue :: Array Boolean -> Boolean
allTrue = foldl (&&) true
@DonaldKellett
DonaldKellett / FileOperations.purs
Created January 1, 2019 14:32
PureScript by Example - 4.17 Listing All Files - Exercise 1-3 Solutions
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
@DonaldKellett
DonaldKellett / Guards.purs
Created January 2, 2019 08:32
PureScript by Example - 5.5 Guards - Exercise 1-2 Solutions
module Guards where
import Prelude
-- Exercise 1
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)
-- Exercise 2
@DonaldKellett
DonaldKellett / NamedPatterns.purs
Created January 2, 2019 09:16
PureScript by Example - 5.9 Named Patterns - Exercise 1,3 Solutions
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