Answered step by step
Verified Expert Solution
Question
1 Approved Answer
needs to be coded using Haskell module Ex06_FlowPatternMatchingSpec ( spec ) where import Test.Hspec import Test.Hspec import Test.QuickCheck import Prelude. Unicode import Data. Monoid. Unicode
needs to be coded using Haskell
module Ex06_FlowPatternMatchingSpec ( spec ) where import Test.Hspec import Test.Hspec import Test.QuickCheck import Prelude. Unicode import Data. Monoid. Unicode factorial :: Int -> Int factorial x | x String tell [] = "This list is empty" tell [x] = "This list has one element: " ++ show x tell (x:y:[]) = "This list has two elements: ++ show x ++ tell (x:y:xs) = "This list is too long" " and " ++ show y length' [] = 0 Tength' (x:xs) = 1 + length' xs sum' [] = 0 sum' (x:xs) = x + sum' xs firstLetter [] = "Empty string, whoops!" firstLetter l@(x:xs) = "The first letter of " ++ 1 ++ " is " ++ [x] main i 19 (1) tell :: [Int] -> String tell [] = "This list is empty" tell [x] = "This list has one element: " ++ show x tell (x:y:[]) = "This list has two elements: " ++ show x ++ tell (x:y:xs) = "This list is too long" " and " ++ show y length' [] = 0 length' (x:xs) = 1 + length' xs sum' [] = 0 sum' (x:xs) = x + sum' xs firstLetter [] = "Empty string, whoops!" firstLetter 1(x:xs) = "The first letter of " ++ 1 ++ " is " ++ [x] main :: 10 () main = hspec spec spec :: Spec spec = do describe "Pattern matching" $ do it "can be used in factorial calc" $ do factorial 5 'shouldBe' 120 it "can fail when no default case" $ do charName 'a' shouldBe "Albert" -- charName'd' 'shouldThrow' PatternMatchFail it "can be used on tuples" $ do pending it "can be used on triples" $ do pending it "can pattern list comprehensions" $ do pending it "can be used for the head function" $ do pending it "can safely process a list" $ do pending it "can count elements in list with recursion" $ do pending it "can reduce add a list" $ do pending it "can hold the original item with pattern" $ do pendingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started