• Home
  • Blog
  • Haskell Programming, computer science homework help

Haskell Programming, computer science homework help

0 comments

I need the following exercise’s completed in the programming language “Haskell”.

Put all the exercise’s into a file called “ps.hs” to run it.

A Makefile is also uploaded, however it is in the form of a “.doc” file, remove it and you should be able to run it.

There is also a Test file uploaded along with it. However to use the Test file you must make it a “.t” file by renaming it and putting it at the end.

#1: Write a function “zeroIsNothing” which

–> converts 0 to Nothing, and any other number to Just that number

–> also write its type signature

zeroIsNothing z

| otherwise = 1

#2: Write a function “nothingIsZero” which

— converts Nothing to 0, and (Just a) to the number a

— also write its type signature

nothingIsZero a = a

#3: Create a typeclass called NumOrString that can contain either an Integer or String object.

— make sure your NumOrString class includes “deriving (Show)” so that

— it may be printed

— write a function makeNum

— which accepts an Integer and produces a NumOrString object that

— encapsulates its value

— also write its type signature

makeNum a = a

#4: Write a function getNum which accepts a NumOrString object

— produces a Maybe object, where

— if the NumOrString object is a number, it should be “Just <that

— number>”

— otherwise, Nothing

— also write its type signature

getNum a = a

#5: Write a function makeString

— which accepts an String and produces a NumOrString object that

— encapsulates its value

— also write its type signature

makeString a = a

#6: Write a function getString which accepts a NumOrString object

— produces a Maybe object, where

— if the NumOrString object is a String, it should be “Just <that

— string>”

— otherwise, Nothing

— also write its type signature

getString a = a

#7: Write a function named “makeNumList” which accepts a numeric argument

— and outputs a list of that many NumOrString objects, counting up

— from 1 to the argument

— e.g. “makeNumList 5” should produce:

— [<NumOrString object with numeric 1>, <NumOrString object with numeric 2>, <NumOrString object with numeric 3>, <NumOrString object with numeric 4>, <NumOrString object with numeric 5>]

— and where

— map getNum <that list> should be [Just 1,Just 2,Just 3,Just 4,Just 5]

— to get the list in the right order, you may need to use Haskell’s append operator, which is ++

— note the type signature of (++) is

— (++) :: [a] -> [a] -> [a]

makeNumList n = [1, 2, 3]

#8: Let’s write bucket in Haskell

— it takes a list of numbers and outputs a list of lists-of-numbers,

— where like numbers are grouped together.

— e.g. bucket [1,1,3,3,3,5,4,4] is
[[1,1],[3,3,3],[5],[4,4]]

— use foldr (fold from the right)

— think about the base case; in this example,

— you’re combining 4 and [] (and you should output [[4]])

— then next you’ll be combining 4 and [[4]]

— and you should output [[4,4]]

— then next you’ll be combining 5 and [[4,4]]

— and you should output [[5],[4,4]]

— define the bucket op fcn first

— name it bucket_op, and write its type signature

bucket_op n l = []

— now write the main bucket (which must use foldr and bucket_op)

bucket l = []

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}