I need the functions to be defined using Haskell language. med3 a b c returns the median value of the set {a,b,c}. med3 :: Integer -> Integer -> Integer -> Integer med3 a b c = undefined -- ghci> med3 1 8 17 -- 8 -- ghci> med3 25 (-1) 6 -- 6 -- ghci> med3 2 11 2 -- 2 blur produces a list containing the means of each pair of numbers in a list. That is, the average of the first and second, the average of the second and third, the average of the third and fourth, and so forth.   Note: It returns an empty list if the input has fewer than two elements. blur :: [Double] -> [Double] blur l = undefined -- ghci> blur [1, 3, 5, 7] -- [2.0,4.0,6.0] -- ghci> blur [7, 8, 2.5, 16, 45, 45, 0] -- [7.5,5.25,9.25,30.5,45.0,22.5] -- ghci> blur [] -- [] -- ghci> blur [1] -- []   Note: As this function uses Double, use / rather than div for division.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need the functions to be defined using Haskell language.

med3 a b c returns the median value of the set {a,b,c}.
med3 :: Integer -> Integer -> Integer -> Integer
med3 a b c = undefined

-- ghci> med3 1 8 17
-- 8
-- ghci> med3 25 (-1) 6
-- 6
-- ghci> med3 2 11 2
-- 2

blur produces a list containing the means of each pair of numbers in a list. That is, the average of the first and second, the average of the second and third, the average of the third and fourth, and so forth.
 
Note: It returns an empty list if the input has fewer than two elements.
blur :: [Double] -> [Double]
blur l = undefined
-- ghci> blur [1, 3, 5, 7]
-- [2.0,4.0,6.0]
-- ghci> blur [7, 8, 2.5, 16, 45, 45, 0]
-- [7.5,5.25,9.25,30.5,45.0,22.5]
-- ghci> blur []
-- []
-- ghci> blur [1]
-- []
 
Note: As this function uses Double, use / rather than div for division.
Expert Solution
Step 1: The Algorithm of the code

 `med3` Function Algorithm:

1. Define a function called `med3` that takes three Integer arguments: `a`, `b`, and `c`.
2. In the function, use pattern matching and guards to handle different cases based on the values of `a`, `b`, and `c`.
3. Check which of the three values, `a`, `b`, or `c`, is the median.
   - If `b` is the median, return `b`.
   - If `a` is the median, return `a`.
   - If `c` is the median, return `c`.

`blur` Function Algorithm:

1. Define a function called `blur` that takes a list of Doubles, denoted as `l`.
2. Use pattern matching to define different cases for the input list:
   - If the input list `l` is empty (`[]`), return an empty list (`[]`) since there are no elements to blur.
   - If the input list `l` has only one element (i.e., `[x]`), return an empty list (`[]`) because there are not enough elements to form pairs for blurring.
   - For the general case, where `l` has at least two elements:
     - Deconstruct the list into the first element `x` and the second element `y`, and the rest of the list (`rest`).
     - Calculate the average of `x` and `y` by adding them together and dividing by 2: `(x + y) / 2`.
     - Prepend the calculated average to the result of recursively calling the `blur` function on the rest of the list `rest`.
3. Continue this process recursively until the list is reduced to one or zero elements, appending averages of adjacent pairs along the way.
4. The function returns a list of averages.

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education