Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up most warnings, apply some hlint #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/BinaryTree/BinarySearchTree.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module BinaryTree.BinarySearchTree where

import Prelude hiding (elem)

data BTree a = Empty | Node a (BTree a) (BTree a) deriving (Show)
data Side = LeftSide | RightSide deriving (Eq, Show)

Expand All @@ -12,7 +14,7 @@ nodeKey (Node x _ _) = Just x
-- Cormen, Thomas H., et al. Introduction to algorithms. pg. 288, MIT press, 2009.
inorderWalk :: (Eq a, Ord a) => BTree a -> [a]
inorderWalk Empty = []
inorderWalk (Node x l r) = (inorderWalk l) ++ [x] ++ (inorderWalk r)
inorderWalk (Node x l r) = inorderWalk l ++ [x] ++ inorderWalk r

-- Function to insert a value into the tree. Returns the new tree.
-- Cormen, Thomas H., et al. Introduction to algorithms. pg. 294, MIT press, 2009.
Expand All @@ -26,21 +28,22 @@ bstInsert (Node x l r) z
bstMax :: (Eq a, Ord a) => BTree a -> Maybe a
bstMax Empty = Nothing
bstMax (Node x Empty Empty) = Just x
bstMax (Node x l Empty) = Just x
bstMax (Node x l r) = bstMax r
bstMax (Node x _ Empty) = Just x
bstMax (Node _ _ r) = bstMax r

-- Function to find the minimum value in the BST.
bstMin :: (Eq a, Ord a) => BTree a -> Maybe a
bstMin Empty = Nothing
bstMin (Node x Empty Empty) = Just x
bstMin (Node x Empty r) = Just x
bstMin (Node x l r) = bstMin l
bstMin (Node x Empty _) = Just x
bstMin (Node _ l _) = bstMin l

-- Function to build BST from a list of values using a fold.
bstFromList :: (Eq a, Ord a) => [a] -> BTree a
bstFromList [] = Empty
bstFromList lst = foldl (\tree elem -> bstInsert tree elem) Empty lst
bstFromList lst = foldl bstInsert Empty lst

sampleTree :: (Eq a, Ord a, Num a) => BTree a
sampleTree = bstFromList [10, 7, 3, 11, 12, 1, 3, 2]

-- Function to check if a given tree is a Binary Search Tree.
Expand All @@ -51,7 +54,7 @@ sampleTree = bstFromList [10, 7, 3, 11, 12, 1, 3, 2]
-- Cormen, Thomas H., et al. Introduction to algorithms. MIT press, 2009.
isBST :: (Ord a, Eq a) => BTree a -> Bool
isBST Empty = True
isBST (Node x Empty Empty) = True
isBST (Node x Empty r) = (x < (nkey r)) && (isBST r) where nkey = (\(Node n ll rr) -> n)
isBST (Node x l Empty) = (x >= (nkey l)) && (isBST l) where nkey = (\(Node n ll rr) -> n)
isBST (Node x l r) = (x >= (nkey l)) && (x < (nkey r)) && (isBST l) && (isBST r) where nkey = (\(Node n ll rr) -> n)
isBST (Node _ Empty Empty) = True
isBST (Node x Empty r) = (x < (nkey r)) && (isBST r) where nkey = (\(Node n _ _) -> n)
isBST (Node x l Empty) = (x >= (nkey l)) && (isBST l) where nkey = (\(Node n _ _) -> n)
isBST (Node x l r) = (x >= (nkey l)) && (x < (nkey r)) && (isBST l) && (isBST r) where nkey = (\(Node n _ _) -> n)
8 changes: 3 additions & 5 deletions src/BinaryTree/BinaryTree.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module BinaryTree.BinaryTree where

import qualified Data.List as L

data BTree a = Empty | Node a (BTree a) (BTree a) deriving (Show)
data Side = LeftSide | RightSide deriving (Eq, Show)

Expand Down Expand Up @@ -38,7 +36,7 @@ bfsList t = concat $ takeWhile (\l -> (length l) > 0) [getLevel i 0 t | i <- [0.
-- Get all nodes from a single level in the tree.
getLevel :: (Num b, Enum b, Eq b) => b -> b -> BTree a -> [a]
getLevel _ _ Empty = []
getLevel 0 _ (Node n l r) = [n]
getLevel 0 _ (Node n _ _) = [n]
getLevel level i (Node n l r)
| i == level = [n]
| otherwise = (getLevel level (i+1) l) ++ (getLevel level (i+1) r)
Expand All @@ -58,7 +56,7 @@ fromList lst = fromListInt 0 lst
-- Internal function to convert list to tree.
fromListInt :: Int -> [a] -> BTree a
fromListInt _ [] = Empty
fromListInt i lst@(x:xs) = Node x (fromListInt (2*i + 1) (drop (i+1) lst))
fromListInt i lst@(x:_) = Node x (fromListInt (2*i + 1) (drop (i+1) lst))
(fromListInt (2*i + 2) (drop (i+2) lst))

-- Count number of nodes in the tree.
Expand All @@ -68,4 +66,4 @@ numNodes t = length $ bfsList t
-- Pretty Print a Binary Tree
simplePrint :: (Show a) => BTree a -> String
simplePrint Empty = ""
simplePrint t = (nodeShow t) ++ " " ++ (simplePrint $ getLeftTree t) ++ (simplePrint $ getRightTree t)
simplePrint t = (nodeShow t) ++ " " ++ (simplePrint $ getLeftTree t) ++ (simplePrint $ getRightTree t)
1 change: 1 addition & 0 deletions src/Graph/Dfs.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Graph.Dfs where

import Data.List
import Prelude hiding (odd, even)

type Node = Int
type Edge = (Node,Node)
Expand Down
32 changes: 20 additions & 12 deletions src/Misc/NQueens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,50 @@ row.

import Data.List (permutations)

main = nqueens 8
nqueens size = mapM_ (printBoard size) $ take 1 $ filter (evaluateBoard size) $ board_permutations size
main :: IO ()
main = nQueens 8

nQueens :: Int -> IO ()
nQueens size = mapM_ (printBoard size) $ take 1 $ filter (evaluateBoard size) $ boardPermutations size

--N sized Chess boards are represented as a one-dimension array.
board_permutations size = permutations [0..size - 1]
boardPermutations :: (Num a, Enum a) => a -> [[a]]
boardPermutations size = permutations [0..size - 1]

--Count the number of valid boards for a specified Chess board size.
count_boards size = length $ filter (evaluateBoard size) $ board_permutations size
countBoards :: (Eq a, Num a, Enum a) => a -> Int
countBoards size = length $ filter (evaluateBoard size) $ boardPermutations size

--Show every valid board
nqueens_list size = mapM_ (printBoard size) $ filter (evaluateBoard size) $ board_permutations size
nQueensList :: Int -> IO ()
nQueensList size = mapM_ (printBoard size) $ filter (evaluateBoard size) $ boardPermutations size

--Board printing function
printBoard size board = do
printBoard2 size board
printBoard :: Int -> [Int] -> IO ()
printBoard size board = do
printBoard2 size board
putStrLn "" where
printBoard2 _ [] = return ()
printBoard2 size board = do
let row = head board
printRow size row
printBoard2 size $ tail board

printRow :: Int -> Int -> IO ()
printRow size row = do
let lstring = (replicate row ". ")
let lstring = replicate row ". "
let rstring = replicate (size - row - 1) ". "
putStrLn $ concat (lstring ++ ["Q "] ++ rstring)
return ()

--Recursively check that prior rows are valid.
evaluateBoard :: (Eq a, Num a) => t -> [a] -> Bool
evaluateBoard _ [] = True
evaluateBoard size rows = (evaluateBoard size $ cut_last rows) && validate size (cut_last rows) (last_row - 1) (last_row + 1) last_row where
evaluateBoard size rows = evaluateBoard size (init rows) && validate size (init rows) (last_row - 1) (last_row + 1) last_row where
last_row = last rows

--Validate that a Queen on a row doesn't have conflicts with earlier rows.
validate :: (Eq b, Num b) => a -> [b] -> b -> b -> b -> Bool
validate _ [] _ _ _ = True
validate size rows left right position = if check_row == left || check_row == right || check_row == position then False else validate size (cut_last rows) (left - 1) (right + 1) position where
validate size rows left right position = not (check_row == left || check_row == right || check_row == position) && validate size (init rows) (left - 1) (right + 1) position where
check_row = last rows

cut_last x = reverse $ drop 1 $ reverse x
9 changes: 5 additions & 4 deletions src/Misc/Powerset.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module Misc.Powerset where

powerset :: [a] -> [[a]]
powerset [] = [[]]
powerset (x:xs) = (powerset xs) ++ (map (\ys -> x:ys) (powerset xs))
powerset (x:xs) = powerset xs ++ map (x :) (powerset xs)

xs = [1,2,3,4]
sampleData :: [Int]
sampleData = [1,2,3,4]

main :: IO ()
main = do
putStrLn (show (powerset xs))
main =
print (powerset sampleData)
4 changes: 3 additions & 1 deletion src/ProjectEuler/Problem1/Problem1.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module ProjectEuler.Problem1.Problem1 where

solList :: [Integer]
solList = filter (\n -> (rem n 5 == 0) || (rem n 3 == 0)) [1..999]

main :: IO ()
main = do
print $ sum solList
print $ sum solList
5 changes: 3 additions & 2 deletions src/ProjectEuler/Problem2/Problem2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ fib n
| n < 0 = []
| n == 1 = [0]
| n == 2 = [0, 1]
| otherwise = reverse $ foldl (\acc n -> (sum (take 2 acc)):acc) [1, 0] [3..n]
| otherwise = reverse $ foldl (\acc _ -> sum (take 2 acc) : acc) [1, 0] [3..n]

main :: IO ()
main = do
print $ sum $ filter even $ takeWhile (<=4000000) (fib 100)
print $ sum $ filter even $ takeWhile (<=4000000) (fib 100)
3 changes: 2 additions & 1 deletion src/ProjectEuler/Problem3/Problem3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module ProjectEuler.Problem3.Problem3 where
largestPrimeFactor :: Integer -> Integer -> Integer
largestPrimeFactor divi val
| val `mod` divi == 0 = if divi == val then val else largestPrimeFactor 2 $ val `div` divi
| val `mod` divi /= 0 = largestPrimeFactor (divi + 1) val
| otherwise = largestPrimeFactor (divi + 1) val

main :: IO ()
main = do
print $ largestPrimeFactor 2 600851475143
1 change: 1 addition & 0 deletions src/ProjectEuler/Problem4/Problem4.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ isPalindrome i =
back = take (length strInt `div` 2) $ reverse strInt
in front == back

main :: IO ()
main = do
print $ maximum [i * j | i <- [100..999], j <- [100..999], isPalindrome (i * j)]
2 changes: 1 addition & 1 deletion src/ProjectEuler/Problem6/Problem6.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ squareSum = (^2) . sum
main :: IO ()
main = do
let l = [1..100]
putStrLn $ show $ (squareSum l) - (sumSquare l)
print $ squareSum l - sumSquare l
6 changes: 4 additions & 2 deletions src/ProjectEuler/Problem7/Problem7.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module ProjectEuler.Problem7.Problem7 where

primes :: [Integer]
primes = sieve [2..]
where sieve (p:xs) = p : sieve [x | x <- xs, rem x p > 0]
where
sieve [] = []
sieve (p:xs) = p : sieve [x | x <- xs, rem x p > 0]

main :: IO ()
main = do
putStrLn $ show $ primes !! 10000 -- zero indexing
print $ primes !! 10000 -- zero indexing
11 changes: 6 additions & 5 deletions src/Robotics/ComplementaryFilter/CompFilt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,29 @@ getAccel s = if length s >= 6

-- Extract gyro data from a lsit of floats
getGyro :: (RealFloat a) => [a] -> (a, a, a)
getGyro s = if length s >= 6
getGyro s = if length s >= 6
then (s!!3, s!!4, s!!5)
else (0, 0, 0)

-- Function to calculate tilt angle from accelerometer reading.
-- By default the tilt measurement is made around the Z axis.
accelTiltAngle :: (RealFloat a) => (a, a, a) -> a
accelTiltAngle (_, y, z) = (atan2 z y)*180.0/pi
accelTiltAngle (_, y, z) = atan2 z y * 180.0/pi


-- Complementary filter, uses the scanl pattern.
compFilt :: (RealFloat a) => [a] -> [a] -> a -> a -> [a]
compFilt ωs θ_accs α δt = scanl (\θ (ω, θ_acc) -> α*(θ + ω*δt) + (1-α)*θ_acc)
compFilt ωs θ_accs α δt = scanl (\θ (ω, θ_acc) -> α*(θ + ω*δt) + (1-α)*θ_acc)
(head θ_accs)
(zip ωs θ_accs)

-- Calculate tilts
calcTilt :: (RealFloat a) => [(a, a, a)] -> [(a, a, a)] -> a -> a -> [a]
calcTilt accel gyro α δt = compFilt (map getX gyro) (map accelTiltAngle accel) α δt
calcTilt accel gyro = compFilt (map getX gyro) (map accelTiltAngle accel)

main :: IO ()
main = do
let accels = map getAccel testData
let gyros = map getGyro testData
let tilts = calcTilt accels gyros 0.95 0.01
print tilts
print tilts
3 changes: 2 additions & 1 deletion src/Robotics/ComplementaryFilter/TestData.hs

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/Sorts/BubbleSort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ bubblePass :: (Ord a) => [a] -> [a]
bubblePass [] = [] -- Empty list is empty.
bubblePass [x] = [x] -- Singleton list is always trivially sorted.
bubblePass (x1:x2:xs) = if x1 > x2
then [x2] ++ (bubblePass ([x1] ++ xs))
else [x1] ++ (bubblePass ([x2] ++ xs))
then x2 : bubblePass (x1 : xs)
else x1 : bubblePass (x2 : xs)

main :: IO ()
main = do
putStrLn $ "Unsorted: " ++ show listToSort
putStrLn $ "Sorted: " ++ show (bubbleSort listToSort)
putStrLn $ "Sorted: " ++ show (bubbleSort listToSort)
4 changes: 3 additions & 1 deletion src/Sorts/InsertionSort.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Sorts.InsertionSort where

listToSort :: [Int]
listToSort = [13, 2, 3, 14, 17, 4, 1, 5, 16, 12, 9, 10, 15, 8, 7, 11, 18, 19, 6, 20]

insertionSort:: (Ord a) => [a] -> [a]
Expand All @@ -11,9 +12,10 @@ insertionSort (x:xs) = insert x (insertionSort xs)
-- and inserts the first argument in the appropriate position
insert :: (Ord a) => a -> [a] -> [a]
insert x [] = [x]
insert x lst@(y:ys) = if x <= y then x:lst else y:(insert x ys)
insert x lst@(y:ys) = if x <= y then x : lst else y : insert x ys


main :: IO ()
main = do
putStrLn $ "Unsorted: " ++ show listToSort
putStrLn $ "Sorted: " ++ show (insertionSort listToSort)
14 changes: 8 additions & 6 deletions src/Sorts/MergeSort.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
module Sorts.MergeSort where

listToSort :: [Int]
listToSort = [13, 2, 3, 14, 17, 4, 1, 5, 16, 12, 9, 10, 15, 8, 7, 11, 18, 19, 6, 20]

mergeSort :: (Ord a) => [a] -> [a]
mergeSort [] = [] -- Empty list is empty
mergeSort [x] = [x] -- Singleton lists are trivially sorted.
mergeSort [x, y] = [(min x y), (max x y)]
mergeSort [x, y] = [min x y, max x y]
mergeSort lst = merge (mergeSort leftL) (mergeSort rightL)
where leftL = take splitPoint lst
rightL = drop splitPoint lst
splitPoint = (length lst) `div` 2
splitPoint = length lst `div` 2

-- Function to execute a merge of two sorted lists
merge :: (Ord a) => [a] -> [a] -> [a]
merge l1 [] = l1
merge [] l2 = l2
merge lst1@(x:xs) lst2@(y:ys) = if x < y
then x:(merge xs lst2)
else y:(merge lst1 ys)
merge lst1@(x:xs) lst2@(y:ys) = if x < y
then x:merge xs lst2
else y:merge lst1 ys

main :: IO ()
main = do
putStrLn $ "Unsorted: " ++ show listToSort
putStrLn $ "Sorted: " ++ show (mergeSort listToSort)
putStrLn $ "Sorted: " ++ show (mergeSort listToSort)
3 changes: 2 additions & 1 deletion src/Sorts/QuickSort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ listToSort = [13, 2, 3, 14, 17, 4, 1, 5, 16, 12, 9, 10, 15, 8, 7, 11, 18, 19, 6,
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = [] -- Empty list is empty.
quicksort [x] = [x] -- Singleton list is always trivially sorted.
quicksort [x, y] = [(min x y), (max x y)]
quicksort [x, y] = [min x y, max x y]
quicksort (x:xs) =
quicksort [a | a <- xs, a <= x] ++ [x] ++ quicksort [a | a <- xs, a > x]
-- x is the pivot, left quicksort returns smaller sorted and right quicksort bigger sorted

main :: IO ()
main = do
putStrLn $ "Unsorted: " ++ show listToSort
putStrLn $ "Sorted: " ++ show (quicksort listToSort)
Expand Down
3 changes: 2 additions & 1 deletion src/Sorts/SelectionSort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ leastUnsorted (x:xs) =
let (y, ys) = leastUnsorted xs
in if x <= y then (x, xs) else (y, x:ys)

main :: IO ()
main = do
putStrLn $ "Unsorted: " ++ show listToSort
putStrLn $ "Sorted: " ++ show (selectionSort listToSort)
putStrLn $ "Sorted: " ++ show (selectionSort listToSort)