-- Name: Stefan Russell -- Date: 9/1/23 -- Class: COP 4020 {- Assignment Instructions: 1. (33 pts) Create a function named problem1. Use a list comprehension for the main function. This function will take a single integer parameter. It indicates the number of primes that will be the final list. The final solution will output a list of every other prime. problem1 10 --> [2,5,11,17,23,31,41,47,59,67] problem1 7 --> [2,5,11,17,23,31,41] 2. (33 pts) Create a function named problem2. Use recursion to solve this problem. This function will take a single integer parameter. It indicates the maximum value of Fibonacci numbers that will be in the list. The final solution will be a list of Fibonacci numbers that have a three as their right-most digit and are less than or equal to n. (For this problem, Fibonacci starts 1,1,2,…) problem2 100 --> [3,13] problem2 10000 --> [3,13,233] 3. (34 pts) Create a function named problem3. For this problem, you can solve it any way you’d like. You may (and should) use helper functions. Create a list from numbers from 1 to n. The numbers must be either a multiple of five or have exactly three factors. problem3 100 --> [4,5,9,10,15,20,25,30,35,40,45,49,50,55,60,65,70,75,80,85,90,95,100] -} {- How to interpret: In order to run Haskell code, make sure to 'ghci' then ':l secondAssignment.hs' then use the function you want to call: Example: problem1 10 problem1 7 problem2 100 problem2 10000 problem3 100 -} --------------------Question1-------------------- -- Helper function to check if its prime (Given from class) checkIfPrime n = ip n [2..(isqrt (n))] where ip _ [] = True ip n (x:xs) | n `mod` x == 0 = False | otherwise = ip n xs isqrt :: Integral i => i -> i isqrt = floor . sqrt . fromIntegral -- Helper function to list every prime number listOfPrimes n = take (2*n) [x | x <- [1..], checkIfPrime x] -- Actual function to take in an integer and get fluctuating primes problem1 n = skipEvenNumOfPrimes (listOfPrimes n) where skipEvenNumOfPrimes (x:y:xs) = y : skipEvenNumOfPrimes xs skipEvenNumOfPrimes _ = [] --------------------Question2-------------------- -- Actual function (Partially from class) problem2 n = go n 1 1 --Given-- where --Given-- go n f s --Given-- | (f+s) > n = [] --Given-- | (f+s) `mod` 10 == 3 = (f+s) : go n s (f+s) --In order to find all right most 3 #'s-- | otherwise = go n s (f+s) --Given-- --------------------Question3-------------------- -- Helper function checks if the number of factors for a number factorsP n = sum([ 1 | x <- [1..n], n `mod` x == 0]) -- Actual function checks if the number is a multiple of 5 or has 3 factors problem3 n = [x | x <- [1..n], (x `mod` 5 == 0 || factorsP x == 3)]
Write, Run & Share Haskell code online using OneCompiler's Haskell online compiler for free. It's one of the robust, feature-rich online compilers for Haskell language, running the latest Haskell version 8.6. Getting started with the OneCompiler's Haskell editor is easy and fast. The editor shows sample boilerplate code when you choose language as Haskell and start coding.
OneCompiler's Haskell online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Haskell program which takes name as input and prints hello message with your name.
main = do
name <- getLine
putStrLn ("Hello " ++ name ++ ", Happy learning!")
Haskell is purely a functional programming language which was introduced in 1990's.
Data-type | Description |
---|---|
Numbers | Haskell is intelligent to identify numbers without specifying data type |
Characters | Haskell is intelligent to identify characters and strings without specifying data type |
Tuple | To declare multiple values in a single data type. Tuples are represented in single paranthesis. For example (10, 20, 'apple') |
Boolean | To represent boolean values, true or false |
List | To declare same type of values in a single data type. Lists are represented in square braces.For example [1, 2, 3] or `['a','b','c','d'] |
When ever you want to perform a set of operations based on a condition or set of conditions, then If-Else/ Nested-If-Else are used.
main = do
let age = 21
if age > 18
then putStrLn "Adult"
else putStrLn "child"
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Functions play an important role in Haskell, since it is a purely functional language.
multiply :: Integer -> Integer -> Integer --declaration of a function
multiply x1 x2 = x1 * x2 --definition of a function
main = do
putStrLn "Multiplication value is:"
print(multiply 10 5) --calling a function