Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit b13d1e97c8ff171bf1e2892b52f38f0b99e7ab12 0 parents
@duckinator authored
91 card_types.hs
@@ -0,0 +1,91 @@
+import System.Random -- give us a way to generate random numbers!
+
+data CardNumber = Two | Three | Four | Five | Six
+ | Seven | Eight | Nine | Ten | Knight
+ | Queen | King | Ace
+ deriving (Bounded, Enum, Eq)
+
+data CardSuit = Club | Heart | Diamond | Spade deriving (Bounded, Enum, Eq)
+
+data Card = Card CardNumber CardSuit deriving (Eq)
+
+type Deck = [Card]
+
+instance Show CardNumber where
+ show Two = "2"
+ show Three = "3"
+ show Four = "4"
+ show Five = "5"
+ show Six = "6"
+ show Seven = "7"
+ show Eight = "8"
+ show Nine = "9"
+ show Ten = "10"
+ show Knight = "Kn"
+ show Queen = "Q"
+ show King = "K"
+ show Ace = "A"
+
+instance Show CardSuit where
+ show Club = ""
+ show Heart = ""
+ show Diamond = ""
+ show Spade = ""
+
+instance Show Card where
+ show (Card n s) = (show n) ++ (show s)
+
+{--
+draw :: Deck -> (Card, Deck)
+draw [] = undefined
+draw (x:xs) = (x, xs)
+--}
+
+draw :: Deck -> (Card, Deck)
+draw xs = (head $ fst l, snd l)
+ where l = drawN 1 xs
+
+drawN :: Int -> Deck -> (Deck, Deck)
+drawN _ [] = undefined
+drawN n xs
+ | n > length xs = undefined
+ | otherwise = (take n xs, drop n xs)
+
+countDeck :: Deck -> Int
+countDeck cards = length cards
+
+deck :: Deck
+deck = reverse $ deck' [Card Two Club]
+
+deck' :: Deck -> Deck
+deck' (f@(Card Ace Spade):l) = f : l
+deck' (f@(Card cn Spade):l) = deck' (Card (succ cn) Club : f : l)
+deck' (f@(Card cn ct):l) = deck' (Card cn (succ ct) : f : l)
+
+main = do
+ print deck
+ print $ length deck
+ print (Card Two Club == Card Two Heart)
+ print (Card Two Club == Card Two Heart)
+ print $ countDeck [Card Two Club, Card Two Heart]
+ print $ draw deck
+ print $ drawN 10 (deck)
+
+
+
+-- Some notes about design
+{-
+We will treat a deck of cards as a simple haskell list of cards.
+
+We are going to consider the "top" of the deck to be the front of a singly linked list
+That is top (x:xs) = x
+
+That means the bottom of the deck will be the last element of the list. In haskell terms
+we will call the bottom of the deck the same as bottom xs = last xs
+
+-}
+
+-- read "2" -> Two
+-- read "H" -> Heart
+-- read "♥" -> Heart
+-- read "2H" -> Card Two Heart
12 compress.hs
@@ -0,0 +1,12 @@
+compress :: (Eq a) => [a] -> [a]
+compress [] = []
+compress [x] = [x] -- also can do (x:[]) to be clear what the pattern is.
+compress (x:y:xs)
+ | x == y = x : (compress xs)
+ | otherwise = x : y : (compress xs)
+
+main = do
+ print $ compress "abcd"
+ print $ compress "abba"
+ print $ compress "abccd"
+ print $ compress "aaaabbbbccccdddd"
26 dropNth.hs
@@ -0,0 +1,26 @@
+dropNth :: [a] -> Int -> [a]
+dropNth x n = dropNth' x n n
+
+dropNth' :: [a] -> Int -> Int -> [a]
+dropNth' [] _ _ = []
+dropNth' _ 0 _ = []
+dropNth' (x:xs) limit 1 = dropNth' xs limit limit
+dropNth' (x:xs) limit n = [x] ++ dropNth' xs limit (n - 1)
+
+-- dropNth' [1,2,3] 2 2
+-- dropNth' [2,3] 2 1
+-- dropNth' [3] 2 0
+-- dropNth' [] 2 2
+-- <- []
+-- <- []
+-- <- [2]
+-- <- [1,2]
+main = do
+ print $ dropNth "abcdabcda" 1 -- ""
+ print $ dropNth "abcdabcda" 2 -- "acaca"
+ print $ dropNth [1,2,3,4,5,6,7,8] 2 -- [1,3,5,7]
+ print $ dropNth [1,2,3,4,5,6,7,8] 1 -- []
+ print $ dropNth [1,2,3] 2 -- [1,3]
+ print $ dropNth [1,2,3,4] 2 -- [1,3]
+ print $ dropNth ([]::[Char]) 1 -- []
+ print $ dropNth ([]::[Char]) 100 -- []
5 duplicate.hs
@@ -0,0 +1,5 @@
+duplicate :: [a] -> [a]
+duplicate [] = []
+duplicate (x:xs) = [x, x] ++ duplicate xs
+
+main = print $ duplicate "abc"
30 isFileInputPrime.hs
@@ -0,0 +1,30 @@
+import System.Environment
+import System.IO
+
+isPrime :: Int -> Bool
+isPrime n
+ | n <= 1 = False
+ | otherwise = not $ any (\x -> n `mod` x == 0) [2,3..(n-1)]
+
+handleLine :: String -> Maybe Bool
+handleLine numberString =
+ if length numberString == 0
+ then Nothing
+ else let number = read numberString :: Int
+ in Just (isPrime number)
+
+--printLine :: String -> IO ()
+printLine line = do
+ case handleLine line of
+ (Just x) -> putStrLn line >> print x
+ Nothing -> return ()
+
+main :: IO ()
+main = do
+ (filename:_) <- getArgs
+ handle <- openFile filename ReadMode
+ hSetBuffering handle LineBuffering
+ contents <- hGetContents handle
+ mapM_ printLine (lines contents)
+ return ()
+
33 isFileInputPrime_outToFile.hs
@@ -0,0 +1,33 @@
+import System.Environment
+import System.IO
+
+isPrime :: Int -> Bool
+isPrime n
+ | n <= 1 = False
+ | otherwise = not $ any (\x -> n `mod` x == 0) [2,3..(n-1)]
+
+handleLine :: String -> Maybe Bool
+handleLine numberString =
+ if length numberString == 0
+ then Nothing
+ else let number = read numberString :: Int
+ in Just (isPrime number)
+
+--printLine :: String -> IO ()
+printLine handle line = do
+ case handleLine line of
+ (Just x) -> hPutStrLn handle line >> hPrint handle x
+ Nothing -> return ()
+
+main :: IO ()
+main = do
+ (inFile:outFile:_) <- getArgs
+ inHandle <- openFile inFile ReadMode
+ outHandle <- openFile outFile WriteMode
+ hSetBuffering inHandle LineBuffering
+ contents <- hGetContents inHandle
+ mapM_ (\x -> printLine outHandle x) (lines contents)
+ hClose inHandle
+ hClose outHandle
+ return ()
+
18 isInputPrime.hs
@@ -0,0 +1,18 @@
+isPrime :: Int -> Bool
+isPrime n
+ | n <= 1 = False
+ | otherwise = not $ any (\x -> n `mod` x == 0) [2,3..(n-1)]
+
+handleLine :: String -> Maybe Bool
+handleLine numberString =
+ if length numberString == 0
+ then Nothing
+ else let number = read numberString :: Int
+ in Just (isPrime number)
+
+main = do
+ numberString <- getLine
+ case handleLine numberString of
+ (Just x) -> print x >> main
+ Nothing -> return()
+
8 isPalindrome.hs
@@ -0,0 +1,8 @@
+isPalindrome :: (Eq a) => [a] -> Bool
+isPalindrome x = x == reverse x
+
+main = do
+ print $ isPalindrome "abcd"
+ print $ isPalindrome "abba"
+ print $ isPalindrome "aba"
+ print $ isPalindrome ""
11 isPrime.hs
@@ -0,0 +1,11 @@
+isPrime :: Int -> Bool
+isPrime n
+ | n <= 1 = False
+ | otherwise = not $ any (\x -> n `mod` x == 0) [2,3..(n-1)]
+
+main = do
+ print $ isPrime 10 -- False
+ print $ isPrime 1 -- False
+ print $ isPrime 2 -- True
+ print $ isPrime 3 -- True
+ print $ isPrime 13 -- True
11 list_last.hs
@@ -0,0 +1,11 @@
+last' :: [a] -> a
+last' [] = undefined
+last' [x] = x
+last' (x:xs) = last' xs
+
+last'' :: [a] -> a
+last'' = head . reverse
+
+main = do
+ print $ last' "abcd"
+ print $ last'' "abcd"
13 list_lastButOne.hs
@@ -0,0 +1,13 @@
+lastButOne :: [a] -> a
+lastButOne [] = undefined
+lastButOne [x] = undefined
+lastButOne [x, y] = x
+lastButOne (x:xs) = lastButOne xs
+
+
+lastButOne' :: [a] -> a
+lastButOne' = head . tail . reverse
+
+main = do
+ print $ lastButOne "abcd"
+ print $ lastButOne' "abcd"
10 list_length.hs
@@ -0,0 +1,10 @@
+len :: [a] -> Int
+len [x] = 1
+len (x:xs) = 1 + (len xs)
+
+--len' :: [a] -> Int
+--len' [x] = [x
+
+main = do
+ print $ len "abcd"
+-- print $ len' "abcd"
5 list_reverse.hs
@@ -0,0 +1,5 @@
+reverse' :: [a] -> [a]
+reverse' [] = []
+reverse' (x:xs) = reverse' xs ++ [x]
+
+main = print $ reverse' "abcd"
16 numbers.txt
@@ -0,0 +1,16 @@
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
37 points.hs
@@ -0,0 +1,37 @@
+listPoints :: [(Int, Int)] -> [(Int, Int)]
+listPoints ((10, 10):l) = (10, 10) : l
+listPoints (( x, 10):l) = listPoints ((x + 1, 0) : (x, 10) : l)
+listPoints (( x, y):l) = listPoints ((x, y + 1) : (x, y) : l)
+
+data Point = Point Int Int deriving (Eq,Show)
+
+listPoints' :: [Point] -> [Point]
+listPoints' ((Point 10 10):l) = Point 10 10 : l
+listPoints' ((Point x 10):l) = listPoints' (Point (x + 1) 0 : Point x 10 : l)
+listPoints' ((Point x y):l) = listPoints' (Point x (y + 1) : Point x y : l)
+
+-- Make your thing with tuples above work with this datastructure below.
+
+data Point3D = Point3D Int Int Int deriving (Eq, Show)
+
+listPoints3D :: [Point3D] -> [Point3D]
+listPoints3D ((Point3D 10 10 10):l) = Point3D 10 10 10 : l
+listPoints3D ((Point3D x 10 10):l) = listPoints3D (Point3D (x + 1) 0 0 : Point3D x 10 10 : l)
+listPoints3D ((Point3D x y 10):l) = listPoints3D (Point3D x (y + 1) 0 : Point3D x y 10 : l)
+listPoints3D ((Point3D x y z):l) = listPoints3D (Point3D x y (z + 1) : Point3D x y z : l)
+
+
+main = do
+ print . reverse $ listPoints [(0, 0)]
+ print . reverse $ listPoints' [Point 0 0]
+ print . reverse $ listPoints3D [(Point3D 0 0 0)]
+
+
+
+
+{-
+listPoints'' :: [Point3D] -> [Point3D]
+listPoints'' ((Point3D 10 10 10):l) = Point3D 10 10 10 : l
+listPoints'' ((Point3D x 10 10):l) = listPoints'' (Point3D (x + 1) 0 : Point3D x 10 : l)
+listPoints'' ((Point3D x y z):l) = listPoints'' (Point3D x (y + 1) : Point3D x y : l)
+-}
17 splitAt.hs
@@ -0,0 +1,17 @@
+splitAt' :: [a] -> Int -> ([a], [a])
+splitAt' xs n
+ | n < 0 = ([], xs)
+ | n > length xs = (xs, [])
+ | otherwise = splitAt'' ([], xs) n
+
+splitAt'' :: ([a], [a]) -> Int -> ([a], [a])
+splitAt'' (start, end) 0 = (start, end)
+splitAt'' (xs, (y:ys)) n = splitAt'' (xs ++ [y], ys) (n - 1)
+
+main = do
+ print $ splitAt' "abcd" 2 -- ("ab", "cd")
+ print $ splitAt' "abcd" 4 -- ("abcd", "")
+ print $ splitAt' "abcd" 10 -- ("abcd", "")
+ print $ splitAt' "abcd" 0 -- ("", "abcd")
+ print $ splitAt' "" 1000 -- ("", "")
+ print $ splitAt' "" (-1) -- ("", "")
10 substr.hs
@@ -0,0 +1,10 @@
+substr :: String -> Int -> Int -> String
+substr "" _ _ = ""
+substr xs s e
+ | e < s = undefined
+ | s > length xs = ""
+ | otherwise = take (e - s) . drop s $ xs
+
+main = do
+ print $ substr "abcdef" 0 4 -- "abcd"
+ print $ substr "abcdef" 1 5 -- "bcde"
Please sign in to comment.
Something went wrong with that request. Please try again.