Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the template, so please provide the Haskell solutions for #5: -- Homework 3 template module Sentence where -- Grammar for the animal sentence

Here is the template, so please provide the Haskell solutions for #5:

-- Homework 3 template

module Sentence where

-- Grammar for the animal sentence language: -- -- -> [] -- | `and` -- -- -> | `and` -- | `cats` | `dogs` | `ducks` | `bunnies`

-- -> `chase` | `cuddle` | `hug` | `scare` -- -> `silly` | `small` | `old` | `happy`

data Sentence = NVN -- finish noun verb noun sentence | NV -- finish noun verb sentence | And -- finish sentence and sentence | End deriving (Eq,Show)

data Adj = -- finish adjectives deriving (Eq,Show)

data Noun = -- finish | NP Adj Noun -- Noun phrase | NAnd Noun Noun -- Finish noun and noun | Cats -- list of nouns deriving (Eq,Show)

data Verb = Chase | -- finish

deriving (Eq,Show)

-- | The sentence: cats cuddle ducks and dogs cuddle ducks ex1 :: Sentence ex1 = NVN Cats Hug Dogs

ex2 :: Sentence ex2 = NVN (NP Silly Cats) Hug Dogs

ex3 :: Sentence ex3 = NVN (NAnd Dogs Cats) Chase Ducks

ex4 :: Sentence ex4 = NVN (NAnd (NP Silly Dogs) Cats) Chase Ducks

-- | Build a sentence from a noun verb noun. -- | buildS2 Cats Hug Cats -- | NVN Cats Hug Cats

buildS2 :: Noun -> Verb ->Noun-> Sentence -- finish

-- | Build a sentence from a noun verb -- | buildS1 Cats Hug -- | NV Cats Hug

buildS1 :: Noun -> Verb ->Sentence -- finish

-- | Build a noun phrase from an adjective and noun -- | buildNP Silly Dogs -- | NP Silly Dogs

buildNP :: Adj -> Noun -> Noun -- finish

-- | Build a noun conjunction from two nouns -- | buildNAnd Dogs Cats -- | NAnd Dogs Cats

buildNAnd :: Noun -> Noun -> Noun -- finish

-- | Build a sentence that is a conjunction of a list of other sentences. -- | conjunction [ex1, ex2] -- | And (NVN Cats Hug Dogs) (NVN (NP Silly Cats) Hug Dogs) -- conjunction :: [Sentence] -> Sentence conjunction [] = End -- finish

-- | Pretty print a sentence. pretty :: Sentence -> String pretty (NVN s v o) = prettyNoun s ++ " " ++ prettyVerb v ++ " " ++ prettyNoun o pretty (And l r) = pretty l ++ " and " ++ pretty r pretty (NV s v) = prettyNoun s ++ " " ++ prettyVerb v pretty (End) = "."

-- | Pretty print a noun. prettyNoun :: Noun -> String prettyNoun Cats = "cats" -- finish

prettyNoun (NP a n) = prettyAdj a ++ " " ++ prettyNoun n prettyNoun (NAnd m n) = prettyNoun m ++ " and " ++prettyNoun n

-- | Pretty print a verb. prettyVerb :: Verb -> String prettyVerb Chase = "chase" -- finish

-- | Pretty print an adjective. prettyAdj :: Adj -> String prettyAdj Silly = "silly" -- finish

-- | Does the sentence contain only cuddling and hugs? -- | isNice ex2 -- | True isNice :: Sentence -> Bool isNice (NVN _ Chase _) = False isNice (NVN _ Cuddle _) = True -- finish

-- |Count the number of words in a sentence -- | wordCount ex4 -- 6

wordCount :: Sentence -> Int wordCount ( And l r ) = wordCount l + wordCount r -- finish

image text in transcribed

elow is the EBNF grammar for the animal sentence language Note: the nonterminals are in and the terminals are in ''. Using the animal.hs template provided. a) Define the abstract syntax for the animal language as a Haskell data type. b) Provide "pretty printing" functions for the sentences in the language. c) Provide functions to build a sentence. d) Write a function isNice to determine if a sentence only contains the verbs hug and cuddle. e) Write a function to build a sentence that is a conjunction of other sentences. f) Write a function wordCount that computes the number of words in a sentence

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions