An integer x is prime if it is divisible only by 1 and itself. A simple way to determine whether x is prime is through trial division, where we try dividing x by every y ∈ Z + such that y < x, determining that x is prime if there are no divisors other than 1. Using trial division, write a function primes :: Int [Int] that, given a number n, calculates a list of prime numbers up to and including n.

primes n =[x | x <- [1..n] , isPrime x]
	where
		divisors x = [y | y <- [1..x], mod x y == 0]
		isPrime x = length divisors x == 0 

ISBN: For example, in the above example, the check digit x10 is calculated by: (10 ∗ 0) + (9 ∗ 2) + (8 ∗ 6) + (7 ∗ 2) + (6 ∗ 1) + (5 ∗ 6) + (4 ∗ 2) + (3 ∗ 0) + (2 ∗ 9) = 142 142 mod 11 = 10 11 − 10 = 1

isbnCheck :: [Int] -> String
isbnCheck digits = if digit == 10 then "X" else (show digit)
    where
        coefficients = reverse [2..10]
        pairs = zipWith (*) coefficients digits
        summed = sum pairs
        digit = 11 - (summed `mod` 11)

Write a function ntimes that calls a function f supplied as an argument n times on input x (with n and x supplied as arguments too)

ntimes :: (a->a) -> Int -> a -> a
ntimes f 0 x = x
ntimes f n x = ntimes f (n-1) (f x)
 
prop_addm :: Int -> Bool
prop_addm = \m -> if m<0 then True else ntimes (+1) m 0 == m

write a function exceptLast which returns all the elements of a list except the final one, a little like the dual of the tail function.

exceptLast :: [a] -> [a]
exceptLast [] = error "empty list"
exceptLast [x] = []
exceptLast (x:xs) = x:(exceptLast xs)
 
prop_exceptLast :: Eq a => [a] -> Bool
prop_exceptLast = \xs -> if length xs > 0 then (exceptLast xs == reverse (tail (reverse xs))) else True
instance Ord Vehicle where
    compare v1 v2 | (numWheels v1) < (numWheels v2)  = LT
                  | (numWheels v1) == (numWheels v2) = EQ
                  | otherwise = GT
-- Use a random number generator g to return a random item of a list. You can assume that the input list is non-empty
randomElem :: (RandomGen g) => g -> [a] -> (a, g)
randomElem gen xs = (xs !! idx, gen')
    where (idx, gen') = uniformR (0 :: Int, (length xs) - 1) gen
 
-- Take a seed, a non-empty source list, and produces an infinite list of randomly-selected elements from the source list.
randomElements :: Int -> [a] -> [a]
randomElements seed xs = randomList (mkStdGen seed) xs
 
-- Helper function for randomElements
randomList :: StdGen -> [a] -> [a]
randomList gen xs = hd : (randomList gen' xs)
    where (hd, gen') = randomElem gen xs

The flatten :: [[a]] -> [a] function can be implemented as a list comprehension flatten xss = [ x | xs <- xss, x <- xs ] allows us to flatten a nested list. Write flatten using the list monad, both with do-notation and with explicit monadic notation

flatten :: [[a]] -> [a]
flatten xss = xss >>= \xs -> xs >>= \x -> return x
 
flatten xss = do
	xs <- xss
	x <- xs
	return x
myMapM :: Monad m => (a -> m b) -> [a] -> m [b]
myMapM _ [] = return []
myMapM f (x:xs) = do
  this <- (f x)
  rest <- (myMapM f xs)
  return (this:rest)
 
myMapM_ :: Monad m => (a -> m b) -> [a] -> m ()
myMapM_ _ [] = return ()
myMapM_ f (x:xs) = do
  this <- (f x)
  rest <- (myMapM_ f xs)
  return ()
data RoseTree a = Leaf a | Node [RoseTree a]
  deriving (Show,Eq)
 
t = Node [Leaf "comp", Node [Leaf "ut", Node [Leaf "i", Leaf "n", Leaf ""]],Leaf "g"]
 
instance Foldable RoseTree where
  foldMap :: Monoid m => (a -> m) -> RoseTree a -> m
  foldMap f (Leaf x) = (f x)
  foldMap f (Node subtrees) = mconcat (map (foldMap f) subtrees)

Write a function funnyFunc that gets a String value from the Reader, and returns the length of this String. Edit the body of funnyFunc to satisfy this definition.

 
funnyfunc :: Reader String Int
funnyfunc = do
	length <$> ask -- Returns back into reader type

Update the findLoginNameSafely definition below to return the String “anonymous” if the environment name is undefined.

findLoginNameSafely :: IO String
findLoginNameSafely =
  catch
    (getEnv "FOGNAME")
    (\(_ :: IOException) -> return "anonymous")
findLoginNameSafely :: IO String
findLoginNameSafely =
  catch
    (Just <$> getEnv "FOGNAME")
    (\(_ :: IOException) -> return Nothing)

Now we need to change the corresponding reader function to handle Maybe values

maybeFunc :: Reader (Maybe String) (Maybe Int)
maybeFunc = fmap (fmap length) ask
...
maybefunc = pure ( (fmap length) <*> ask )