0% found this document useful (0 votes)
9 views7 pages

Lab Report - 4

The document discusses typeclasses in Haskell and provides examples of using typeclasses to define datatypes for things like days of the week and traffic lights, and functions that operate on those types like checking if a day is a weekday or Wednesday. It also gives examples of deriving instances for types and using typeclasses for operations like sorting, element checking, and printing values.

Uploaded by

dineshvardhan339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Lab Report - 4

The document discusses typeclasses in Haskell and provides examples of using typeclasses to define datatypes for things like days of the week and traffic lights, and functions that operate on those types like checking if a day is a weekday or Wednesday. It also gives examples of deriving instances for types and using typeclasses for operations like sorting, element checking, and printing values.

Uploaded by

dineshvardhan339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

19CSE313 – Principles of Programming Languages

Lab Report – 4

Name : Mridhulaa S S
Roll No: 21040
Class: CSE - A
TYPES AND TYPECLASSES :
1. Create a data type called TrafficLight that stores different traffic colors using sum
type.

Syntax:

Output:

2. Create a record by defining a data type called car that stores company, model, year,
price, and color. Also print it using suitable deriving instance

Syntax:

Output:
3. Create a type Day for days of the week and derive instances for (Eq, Ord, Show,
Read, Bounded, Enum)

i) write two functions:

• isWednesday, which takes a day of the week and returns True if it’s Wednesday
and False otherwise.
• nextDay, which takes a day of the week and returns the day of the week that
comes after it.

Syntax:

Output:

ii) Apply compare, minBound, maxBound, show on type Day.

Output:

4.Write a new function using suitable typeclass to decide if a value is an element of a list.

Syntax:
Output:

5. Write a new function using suitable typeclass to perform insertion sort

Syntax:

Output:

6. Write a function isElem using suitable typeclass and Make sure the following test
cases pass:

isElem 0 [] == False

isElem (error "Nothing") [] == False

isElem 3 [10, 20, 30] == False

isElem 3 [1, 2, 3] == True


Syntax:

Output:

7. Use suitable typeclass and create instances to print the Characters: A|B|C|D|E. (note:
for any other characters it should print as “not printable”)

Syntax:

Output:
8. Create a datatype Weekday for days of the week. Define a function workday which
takes a day of the week and returns True if it’s working and False otherwise.

Syntax:

Output:

9. Create a 'YesNo' type class. Define a function yesno which defines Boolean semantics
for all different types.

Syntax:
Output:

You might also like