Functional Programming in Haskell
42 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the output of the greetPerson function when it is called with person1?

  • Hello, 25!
  • Hello, Alice! (correct)
  • Hello, Person!
  • Hello, Bob!
  • The Person data type has fields for 'name' and 'age'.

    True

    What is the purpose of the greetPerson function?

    To return a greeting message that includes the person's name.

    The first person in the example is named __________.

    <p>Alice</p> Signup and view all the answers

    Match the following example outputs to their corresponding person:

    <p>Hello, Alice! = person1 Hello, Bob! = person2</p> Signup and view all the answers

    What is the output of the function isAdult when person1 is 20?

    <p>True</p> Signup and view all the answers

    The constructor name in Haskell can be the same as the type name without causing confusion.

    <p>False</p> Signup and view all the answers

    What is the minimum age for a person to be considered an adult according to the given function?

    <p>18</p> Signup and view all the answers

    In Haskell, a Shape can be either a Circle with a radius or a __________ with height and width.

    <p>Rectangle</p> Signup and view all the answers

    Match the Haskell data types with their descriptions:

    <p>Person = Represents a person's name and age Shape = Defines geometric figures such as circles and rectangles Individual = Alternative name to avoid naming overlap with Person Circle = Represents a shape defined by its radius</p> Signup and view all the answers

    What is the type of a Basket in Haskell?

    <p>[ShopItem]</p> Signup and view all the answers

    A Basket can contain mixed types of values due to Haskell's type system.

    <p>False</p> Signup and view all the answers

    What are the two components of a ShopItem?

    <p>A String for the item name and an Int for the item price.</p> Signup and view all the answers

    The function countItems returns 0 if the basket is _____?

    <p>empty</p> Signup and view all the answers

    Match the following elements with their descriptions:

    <p>Basket = A list of ShopItems ShopItem = A tuple containing item name and price countItems = Function to count items in a basket Type Alias = A way to create a synonym for a type</p> Signup and view all the answers

    Which component type does Haskell treat the price of a ShopItem as?

    <p>Int</p> Signup and view all the answers

    Haskell's strong typing allows for type mismatches to occur at runtime.

    <p>False</p> Signup and view all the answers

    What does the countItems function use to check how many items there are in a Basket?

    <p>Pattern matching</p> Signup and view all the answers

    What does the function flipH do?

    <p>Flips the picture horizontally</p> Signup and view all the answers

    What is the purpose of the countItems function in the context provided?

    <p>To count each item in a list</p> Signup and view all the answers

    Algebraic Types (ATs) can be defined using the keyword 'define'.

    <p>False</p> Signup and view all the answers

    The function printPicture will modify the original picture.

    <p>False</p> Signup and view all the answers

    What does the function totalPrice return when the basket is empty?

    <p>0</p> Signup and view all the answers

    What type is defined for a Picture?

    <p>Picture = [[Char]]</p> Signup and view all the answers

    An example of an Algebraic Type is data Person = Person { name :: ______, age :: Int }.

    <p>String</p> Signup and view all the answers

    The function beside combines two pictures by placing them ______.

    <p>side-by-side</p> Signup and view all the answers

    Match the following functions with their descriptions:

    <p>printPicture = Displays a picture flipH = Flips the picture horizontally flipV = Flips the picture vertically invertColour = Inverts the colors of the picture</p> Signup and view all the answers

    Match the following functions or concepts with their purposes:

    <p>countItems = Counts items in a basket totalPrice = Calculates total price of items Algebraic Types = Define custom types with named constructors Tuples = Group multiple values together without labels</p> Signup and view all the answers

    Which line is responsible for flipping the picture vertically?

    <p>flipV pic = [ reverse line | line &lt;- pic ]</p> Signup and view all the answers

    Which statement highlights a benefit of using Algebraic Types over Tuples?

    <p>Algebraic Types offer controlled construction.</p> Signup and view all the answers

    The invertColour function uses map to invert each row.

    <p>True</p> Signup and view all the answers

    The totalPrice function adds up the price of each item in the basket.

    <p>True</p> Signup and view all the answers

    What character represents a black pixel in the defined Picture type?

    <h1></h1> Signup and view all the answers

    What is the main advantage of clear labeling in Algebraic Types?

    <p>Improved readability and purpose indication</p> Signup and view all the answers

    What is the output of the function isRound when given a Rectangle?

    <p>False</p> Signup and view all the answers

    The Maybe type can represent a value and an absence of a value.

    <p>True</p> Signup and view all the answers

    What does the function area return for a Circle with radius 3?

    <p>28.274333882308138</p> Signup and view all the answers

    The general form for defining a data type is data Typename = Con1 ______ | Con2 ______.

    <p>t11...t1k1</p> Signup and view all the answers

    Match the following Haskell constructs with their descriptions:

    <p>isRound = Function that checks if a shape is round Maybe = Type that handles optional values area = Function that calculates area of shapes Rectangle = Constructor representing a rectangle shape</p> Signup and view all the answers

    What does area (Rectangle h w) return?

    <p>h * w</p> Signup and view all the answers

    Constructors in Haskell can't take arguments.

    <p>False</p> Signup and view all the answers

    What is a key benefit of using polymorphic types?

    <p>Reusability across types</p> Signup and view all the answers

    Study Notes

    • Functional Programming covers the use of tuples and pattern matching.
    • Learning objectives include tuples, Algebraic Types in Haskell, and Prelude library usage.
    • A custom type alias, ShopItem, is defined as (String, Int).
    • ShopItem allows use cases for items in a shop to be represented using tuples for item names (String) and prices (Int, in cents).
    • Defining items (e.g., "Salt: 1kg", 139) allows for declaring their type as ShopItem.
    • A list of items, a basket, can be demonstrated using ShopItem types.
    • The concept of algebraic types (ATs) in Haskell and how they are defined allows for custom types with named constructors.
    • ATs model data better than primitive types.
    • ATs are defined using the keyword data.
    • Data types like data Bool = True | False are examples of enum types which are helpful for representing sets of values.
    • Data types such as data Person = Person {name :: String, age :: Int} are examples of product types that combine multiple values into a single type.
    • Haskell's prelude includes functions like map, filter, and foldr for data manipulation that operate on types such as lists.
    • map function applies a function to each element in a list.
    • filter function filters elements in a list based on a predicate, returning a list of matched elements.
    • foldr reduces a list to a single value.
    • putStrLn is an I/O function that utilizes monads for sequencing actions.
    • Function composition is possible using the $ operator to chain functions.
    • Pictures can be represented using a 2D grid of characters, with a Picture type defined as [[Char]].
    • printPicture function displays a picture.
    • flipH function horizontally flips a picture.
    • flipV function vertically flips a picture.
    • above function stacks pictures vertically.
    • beside function places pictures side-by-side.
    • Functions can be composed together to create more complex functionality.
    • The Maybe type is for handling potential failures (e.g., safe division).
    • The Maybe type can have a value (using Just) or no value (Nothing).

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    This quiz covers functional programming concepts focusing on tuples, pattern matching, and algebraic types in Haskell. You'll explore how to define custom types like ShopItem and use them to represent items in a shop, as well as the benefits of algebraic types over primitive types. Test your understanding of the Prelude library and data type definitions.

    More Like This

    Use Quizgecko on...
    Browser
    Browser