Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

-- Homework 3 template module Sentence where -- Grammar for the animal sentence language: -- -- -> [ ] -- | `and` -- -- ->

-- 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

Above is the template provided

here is the test script, please make sure it works with the script below

import Sentence main = do let s1 = buildS2 Cats Hug Bunnies let s2 = buildS1 Cats Cuddle let s3 = buildNP Silly Ducks let s4 = buildNAnd Dogs Cats let s5 = conjunction [s2, s1] let s6 = buildS2 s3 Chase s4 let s7 = buildS1 Dogs Scare let s8 = conjunction [s1, s2, s7] let s9 = buildNP Old s3 let s10 = buildS2 s9 Cuddle Cats

putStr " s1 = " print (s1) print (pretty(s1)) putStr" Is nice s1 = " print (isNice s1) putStr" Word count s1 = " print (wordCount s1) putStrLn "*********************" putStr " s2 = " print (s2) print (pretty(s2)) putStr" Is nice s2 = " print (isNice s2) putStr" Word count s2 = " print (wordCount s2) putStrLn "*********************" putStr " s5 = " print (s5) print (pretty(s5)) putStr" Is nice s5 = " print (isNice s5) putStr" Word count s5 = " print (wordCount s5) putStrLn "*********************" putStr " s6 = " print (s6) print (pretty(s6)) putStr" Is nice s6 = " print (isNice s6) putStr" Word count s6 = " print (wordCount s6) putStrLn "*********************" putStr " s7 = " print (s7) print (pretty(s7)) putStr" Is nice s7 = " print (isNice s7) putStr" Word count s7 = " print (wordCount s7) putStrLn "*********************" putStr " s8 = " print (s8) print (pretty(s8)) putStr" Is nice s8 = " print (isNice s8) putStr" Word count s8 = " print (wordCount s8) putStrLn "*********************" putStr " s10 = " print (s10) print (pretty(s10)) putStr" Is nice s10 = " print (isNice s10) putStr" Word count s10 = " print (wordCount s10)

when you run the script, it should output:

image text in transcribed

please make sure it works with the script in Haskell! if you do, I will be sure to upvote, thank you!!image text in transcribed

GHCi, version 7.6.3: http://ww.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 1 of 2] Compiling Sentence (Sentence.hs, interpreted) [2 of 2] Compiling Main ( hw3verifier.hs, interpreted) ok, modules loaded: Sentence, Main. *Main> main s1= NVN Cats Hug Bunnies "cats hug bunnies" Is nice s1= True Word count s1 =3 kkkkkkkkkkk s2= NV Cats Cuddle "cats cuddle" Is nice 52= True Word count s2 =2 55= And (NV Cats Cuddle) (NVN Cats Hug Bunnies) "cats cuddle and cats hug bunnies" Is nice 55= True Word count 55=6 s6 = NVN (NP Silly Ducks) Chase (NAnd Dogs Cats) "silly ducks chase dogs and cats" Is nice s6 =False Word count s6=6 s7 = NV Dogs Scare "dogs scare" Is nice s7 = False Word count s7=2 s8 = And (NVN Cats Hug Bunnies) (And (NV Cats Cuddle) (NV Dogs Scare)) "cats hug bunnies and cats cuddle and dogs scare" Is nice 58= False Word count s8=9 kkkkkkkkkkkkk s10= NVN (NP 0ld (NP Silly Ducks)) Cuddle Cats "old silly ducks cuddle cats" Is nice 510= True Word count s10=5 Mains 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_2

Step: 3

blur-text-image_3

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

Finance The Role Of Data Analytics In Manda Due Diligence

Authors: Ps Publishing

1st Edition

B0CR6SKTQG, 979-8873324675

More Books

Students also viewed these Databases questions