Skip to content

Commit

Permalink
every solution I made
Browse files Browse the repository at this point in the history
  • Loading branch information
Cem-Gulec committed Apr 1, 2024
1 parent e20667b commit 571048f
Show file tree
Hide file tree
Showing 23 changed files with 1,108 additions and 303 deletions.
40 changes: 29 additions & 11 deletions exercises/Set10a.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Mooc.Todo
-- take 10 (doublify [0..]) ==> [0,0,1,1,2,2,3,3,4,4]

doublify :: [a] -> [a]
doublify = todo
doublify = concatMap (\x -> [x,x])

------------------------------------------------------------------------------
-- Ex 2: Implement the function interleave that takes two lists and
Expand All @@ -37,7 +37,9 @@ doublify = todo
-- take 10 (interleave [1..] (repeat 0)) ==> [1,0,2,0,3,0,4,0,5,0]

interleave :: [a] -> [a] -> [a]
interleave = todo
interleave [] ys = ys
interleave xs [] = xs
interleave (x:xs) (y:ys) = x : y : interleave xs ys

------------------------------------------------------------------------------
-- Ex 3: Deal out cards. Given a list of players (strings), and a list
Expand All @@ -56,7 +58,7 @@ interleave = todo
-- Hint: remember the functions cycle and zip?

deal :: [String] -> [String] -> [(String,String)]
deal = todo
deal players cards = zip cards (cycle players)

------------------------------------------------------------------------------
-- Ex 4: Compute a running average. Go through a list of Doubles and
Expand All @@ -71,10 +73,14 @@ deal = todo
-- averages [3,2,1] ==> [3.0,2.5,2.0]
-- take 10 (averages [1..]) ==> [1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5]



averages :: [Double] -> [Double]
averages = todo
averages xs = go xs 0.0 0
where
go [] _ _ = []
go (y:ys) sum count = let newSum = sum + y
newCount = count + 1
avg = newSum / fromIntegral newCount
in avg : go ys newSum newCount

------------------------------------------------------------------------------
-- Ex 5: Given two lists, xs and ys, and an element z, generate an
Expand All @@ -92,7 +98,7 @@ averages = todo
-- take 10 (alternate [1,2] [3,4,5] 0) ==> [1,2,0,3,4,5,0,1,2,0]

alternate :: [a] -> [a] -> a -> [a]
alternate xs ys z = todo
alternate xs ys z = cycle (xs ++ [z] ++ ys ++ [z])

------------------------------------------------------------------------------
-- Ex 6: Check if the length of a list is at least n. Make sure your
Expand All @@ -104,7 +110,7 @@ alternate xs ys z = todo
-- lengthAtLeast 10 [0..] ==> True

lengthAtLeast :: Int -> [a] -> Bool
lengthAtLeast = todo
lengthAtLeast n xs = length (take n xs) == n

------------------------------------------------------------------------------
-- Ex 7: The function chunks should take in a list, and a number n,
Expand All @@ -122,7 +128,9 @@ lengthAtLeast = todo
-- take 4 (chunks 3 [0..]) ==> [[0,1,2],[1,2,3],[2,3,4],[3,4,5]]

chunks :: Int -> [a] -> [[a]]
chunks = todo
chunks n xs
| lengthAtLeast n xs = (take n xs) : chunks n (tail xs)
| otherwise = []

------------------------------------------------------------------------------
-- Ex 8: Define a newtype called IgnoreCase, that wraps a value of
Expand All @@ -138,7 +146,13 @@ chunks = todo
-- ignorecase "abC" == ignorecase "ABc" ==> True
-- ignorecase "acC" == ignorecase "ABc" ==> False

ignorecase = todo
newtype IgnoreCase = IgnoreCase String deriving Show

instance Eq IgnoreCase where
(IgnoreCase s1) == (IgnoreCase s2) = (map toLower s1) == (map toLower s2)

ignorecase :: String -> IgnoreCase
ignorecase = IgnoreCase

------------------------------------------------------------------------------
-- Ex 9: Here's the Room type and some helper functions from the
Expand Down Expand Up @@ -182,4 +196,8 @@ play room (d:ds) = case move room d of Nothing -> [describe room]
Just r -> describe room : play r ds

maze :: Room
maze = todo
maze = let maze1 = Room "Maze" [("Left", maze2), ("Right", maze3)]
maze2 = Room "Deeper in the maze" [("Left", maze3), ("Right", maze1)]
maze3 = Room "Elsewhere in the maze" [("Left", maze1), ("Right", maze2)]
in maze1

23 changes: 17 additions & 6 deletions exercises/Set10b.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import Mooc.Todo
-- False ||| undefined ==> an error!

(|||) :: Bool -> Bool -> Bool
x ||| y = todo
_ ||| True = True
True ||| _ = True
False ||| False = False

------------------------------------------------------------------------------
-- Ex 2: Define the function boolLength, that returns the length of a
Expand All @@ -36,7 +38,9 @@ x ||| y = todo
-- length [False,undefined] ==> 2

boolLength :: [Bool] -> Int
boolLength xs = todo
boolLength [] = 0
boolLength (True:xs) = 1 + boolLength xs
boolLength (False:xs) = 1 + boolLength xs

------------------------------------------------------------------------------
-- Ex 3: Define the function validate which, given a predicate and a
Expand All @@ -50,7 +54,11 @@ boolLength xs = todo
-- validate (\x -> undefined) 3 ==> an error!

validate :: (a -> Bool) -> a -> a
validate predicate value = todo
validate predicate value = case forcePred of
True -> value
False -> value
where
forcePred = predicate value

------------------------------------------------------------------------------
-- Ex 4: Even though we can't implement the generic seq function
Expand Down Expand Up @@ -84,10 +92,13 @@ class MySeq a where
myseq :: a -> b -> b

instance MySeq Bool where
myseq = todo
myseq True b = b
myseq False b = b

instance MySeq Int where
myseq = todo
myseq 0 b = b
myseq n b = let _ = n - n in b

instance MySeq [a] where
myseq = todo
myseq [] b = b
myseq (_:_) b = b
56 changes: 45 additions & 11 deletions exercises/Set11a.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ import Mooc.Todo
-- first line should be HELLO and the second one WORLD

hello :: IO ()
hello = todo
hello = do
putStrLn "HELLO"
putStrLn "WORLD"

------------------------------------------------------------------------------
-- Ex 2: define the IO operation greet that takes a name as an
-- argument and prints a line "HELLO name".

greet :: String -> IO ()
greet name = todo
greet name = putStrLn ("HELLO " ++ name)

------------------------------------------------------------------------------
-- Ex 3: define the IO operation greet2 that reads a name from the
Expand All @@ -42,7 +44,9 @@ greet name = todo
-- Try to use the greet operation in your solution.

greet2 :: IO ()
greet2 = todo
greet2 = do
name <- getLine
greet name

------------------------------------------------------------------------------
-- Ex 4: define the IO operation readWords n which reads n lines from
Expand All @@ -56,7 +60,9 @@ greet2 = todo
-- ["alice","bob","carl"]

readWords :: Int -> IO [String]
readWords n = todo
readWords n = do
wordsList <- replicateM n getLine
return (sort wordsList)

------------------------------------------------------------------------------
-- Ex 5: define the IO operation readUntil f, which reads lines from
Expand All @@ -72,14 +78,21 @@ readWords n = todo
-- STOP
-- ["bananas","garlic","pakchoi"]

readUntil :: (String -> Bool) -> IO [String]
readUntil f = todo
readUntil f = go []
where
go acc = do
line <- getLine
if f line
then return acc
else go (acc ++ [line])

------------------------------------------------------------------------------
-- Ex 6: given n, print the numbers from n to 0, one per line

countdownPrint :: Int -> IO ()
countdownPrint n = todo
countdownPrint n = when (n >= 0) $ do
print n
countdownPrint (n - 1)

------------------------------------------------------------------------------
-- Ex 7: isums n should read n numbers from the user (one per line) and
Expand All @@ -94,15 +107,26 @@ countdownPrint n = todo
-- 5. produces 9

isums :: Int -> IO Int
isums n = todo
isums n = go n 0
where
go 0 s = return s
go k s = do
num <- readLn
let newSum = s + num
print newSum
go (k-1) newSum

------------------------------------------------------------------------------
-- Ex 8: when is a useful function, but its first argument has type
-- Bool. Write a function that behaves similarly but the first
-- argument has type IO Bool.

whenM :: IO Bool -> IO () -> IO ()
whenM cond op = todo
whenM cond op = do
conditionResult <- cond
if conditionResult
then op
else return ()

------------------------------------------------------------------------------
-- Ex 9: implement the while loop. while condition operation should
Expand All @@ -122,7 +146,13 @@ ask = do putStrLn "Y/N?"
return $ line == "Y"

while :: IO Bool -> IO () -> IO ()
while cond op = todo
while cond op = do
conditionResult <- cond
if conditionResult
then do
op
while cond op
else return ()

------------------------------------------------------------------------------
-- Ex 10: given a string and an IO operation, print the string, run
Expand All @@ -142,4 +172,8 @@ while cond op = todo
-- 4. returns the line read from the user

debug :: String -> IO a -> IO a
debug s op = todo
debug s op = do
putStrLn s
result <- op
putStrLn s
return result
51 changes: 44 additions & 7 deletions exercises/Set11b.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import Mooc.Todo
-- "xfoobarquux"

appendAll :: IORef String -> [String] -> IO ()
appendAll = todo
appendAll ref strs = do
currentVal <- readIORef ref
let newVal = currentVal ++ concat strs
writeIORef ref newVal

------------------------------------------------------------------------------
-- Ex 2: Given two IORefs, swap the values stored in them.
Expand All @@ -35,7 +38,12 @@ appendAll = todo
-- "x"

swapIORefs :: IORef a -> IORef a -> IO ()
swapIORefs = todo
swapIORefs ref1 ref2 = do
val1 <- readIORef ref1
val2 <- readIORef ref2

writeIORef ref1 val2
writeIORef ref2 val1

------------------------------------------------------------------------------
-- Ex 3: sometimes one bumps into IO operations that return IO
Expand All @@ -61,7 +69,10 @@ swapIORefs = todo
-- replicateM l getLine

doubleCall :: IO (IO a) -> IO a
doubleCall op = todo
doubleCall op = do
innerOp <- op
result <- innerOp
return result

------------------------------------------------------------------------------
-- Ex 4: implement the analogue of function composition (the (.)
Expand All @@ -80,7 +91,9 @@ doubleCall op = todo
-- 3. return the result (of type b)

compose :: (a -> IO b) -> (c -> IO a) -> c -> IO b
compose op1 op2 c = todo
compose op1 op2 c = do
resultA <- op2 c
op1 resultA

------------------------------------------------------------------------------
-- Ex 5: Reading lines from a file. The module System.IO defines
Expand Down Expand Up @@ -110,7 +123,14 @@ compose op1 op2 c = todo
-- ["module Set11b where","","import Control.Monad"]

hFetchLines :: Handle -> IO [String]
hFetchLines = todo
hFetchLines handle = do
eof <- hIsEOF handle
if eof
then return []
else do
line <- hGetLine handle
rest <- hFetchLines handle
return (line : rest)

------------------------------------------------------------------------------
-- Ex 6: Given a Handle and a list of line indexes, produce the lines
Expand All @@ -123,7 +143,18 @@ hFetchLines = todo
-- handle.

hSelectLines :: Handle -> [Int] -> IO [String]
hSelectLines h nums = todo
hSelectLines h nums = go h nums 1
where
go _ [] _ = return []
go handle (n:ns) currentLine
| n < currentLine = error "indexes should be ascending"
| n == currentLine = do
line <- hGetLine handle
rest <- go handle ns (currentLine + 1)
return (line : rest)
| otherwise = do
_ <- hGetLine handle
go handle (n:ns) (currentLine + 1)

------------------------------------------------------------------------------
-- Ex 7: In this exercise we see how a program can be split into a
Expand Down Expand Up @@ -164,4 +195,10 @@ counter ("print",n) = (True,show n,n)
counter ("quit",n) = (False,"bye bye",n)

interact' :: ((String,st) -> (Bool,String,st)) -> st -> IO st
interact' f state = todo
interact' f state = do
input <- getLine
let (continue, output, newState) = f (input, state)
putStrLn output
if continue
then interact' f newState
else return newState
Loading

0 comments on commit 571048f

Please sign in to comment.