Question

All of the problems must be done in Haskell. You may download a local IDE or...

All of the problems must be done in Haskell. You may download a local IDE or use [this website.][https://repl.it/languages/haskell].

You are not allowed any functions that trivialize a problem!

Here are some examples of lists you can test your solutions on: let list1 = [5, 10, 15, 20, 25, 30]

let list2 = [50, 100, 150, 200, 250, 300]

  1. Write a function that returns the maximum value in a list.
  2. Write a function that returns the nth element in the list.
  3. Write a function that doubles the values in a list.
  4. Write function that accepts an integer and a list. It should return 1 if the integer is in the list, and -1 if it is not.
  5. Write a function that combines two lists into a list of tuples.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1  Write a function that returns the maximum value in a list.

findMax :: [Int] -> Maybe Int findMax [] = Nothing findMax (x:xs) = case (findMax xs) of Nothing -> Just x Just m -> if x > m then Just x else Just m main = print $ show $ findMax [5,10,15,20,25,30]

2. Write a function that returns the nth element in the list.

primes = 2 : sieve [5,10,15,20,25,30] where

sieve [] = []

sieve (p:xs) = p : sieve (xs `minus` [p,p+2*p..

*Main> :t (!!) (!!) :: [a] -> Int -> a

3. Write a function that doubles the values in a list

duplicateEach = (>>= replicate 2) duplicateEach2 = concatMap (replicate 2) duplicateEach3 xs = [ y | x <- xs, y<-[x,x] ] 

4. Write a function that combines two lists into a list of tuples.

zipThree :: [a] -> [b] -> [c] -> Maybe [(a, b, c)] zipThree [] [] [] = Just [] zipThree (x:xs) (y:ys) (z:zs) = case zipThree xs ys zs of Nothing -> Nothing Just as -> Just $ (x, y, z) : as zipThree _ _ _ = Nothing
Add a comment
Know the answer?
Add Answer to:
All of the problems must be done in Haskell. You may download a local IDE or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

  • Deletion of List Elements The del statement removes an element or slice from a list by position....

    Deletion of List Elements The del statement removes an element or slice from a list by position. The list method list.remove(item) removes an element from a list by it value (deletes the first occurance of item) For example: a = ['one', 'two', 'three'] del a[1] for s in a: print(s) b = ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b'] del b[1:5] print(b) b.remove('a') print(b) # Output: ''' one three ['a', 'f', 'a', 'b'] ['f', 'a', 'b'] ''' Your textbook...

  • Assignment 3 In this assignment, you will write a program that plot ASCII text approximations of...

    Assignment 3 In this assignment, you will write a program that plot ASCII text approximations of the Mandelbrot set. Problems Problem 1 Consider the function ?(?,?) defined as follows: ?(?,?)(?, ?) = (?2 − ?2 + ?, 2?? + ?) We define the orbit ?(?, ?) of a point (?, ?) to be an infinite list of items: ?(?, ?) = {(0, 0), ?(?,?)(0, 0), ?(?,?)(?(?,?)(0, 0)), ?(?,?)(?(?,?)(?(?,?)(0, 0))), …} In other words, the nth entry of the list ?(?,...

  • Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with...

    Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with output plesase. LISP Programming Assignment It is a good idea to start this assignment early; Lisp programming, while not inherently difficult, often seem somewhat foreign at first, particularly when it comes to recursion and list manipulation. This assignment is loosely based on material by Dr. Henri Casanova.   Problem #1 Define a function that takes two arguments and returns the greater of the two. Problem...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Part II: Let Me Inn (20 points) You are the manager of the Let Me Inn....

    Part II: Let Me Inn (20 points) You are the manager of the Let Me Inn. A travel agent has sent you a list of reservations that can be accommodated by the hotel. Your primary job is to send him back the total cost of the reservations. To make sure your hotel runs properly, you also will keep track of how much money you have made from each type of room (Normal, Double, King, Suite). Complete the function hotel manager...

  • The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and incl...

    The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and include them in your project. IheじML diagram below details the class Point くくfriend>> ostream& operator.((ostream&, point&) <ごfriend::. İstream& operator:..イ1stream&-point& - : double - v doublc getX) double getYO double - sctX( double): void - set Y(double) : void - point(double-0.0, double-0.0 operator-(const point& bool perator< const...

  • Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such...

    Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such as declaration, initialization, storing, retrieving, and processing elements. • Effectively manipulating and using ArrayList objects. • Becoming familiar with the use of arrays as method arguments and how array elements are passed to and returned from the called method. • Mastering the use of various debugging tools available on the Eclipse IDU. Important: EVERY one of the following Activities MUST be in a separate...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT