CSC 122: Computer Science II, Spring 2005
Haskell Homework
Due Tuesday, March 15, in class
Write the following functions. Turn in a printout of your
functions. If appropriate you may reuse functions that you have
already written on this sheet to write other functions on this
sheet.
- Write a Haskell function sumUp that takes two parameters
(both integers). The function should return the sum of all the
numbers between the two parameters (inclusive). You cannot make any
assumptions about the order of the parameters. For example,
sumUp( 5, 3 ) should return 12.
- Write a Haskell function called count that takes two
parameters (a list and an item). The function should return the
number of times that the given item occurs in the list. For
example, count( [1, 3, 2, 3], 3 ) should return 2.
- Write a Haskell function called remove that takes two
parameters (a list and an item). The function should return a list
that would result from removing the first copy of item from the
list (all other copies of item should remain). If item is not in
the list, return the list unchanged. For example, remove( [2, 4,
6, 4, 8, 10], 4 ) should return [2, 6, 4, 8, 10].
- Write a Haskell function called insert that takes two
parameters (a list and a number). You may assume that the list
consists of 0 or more numbers that are in order. The function
should return a list that would result from inserting the number
into the list into the appropriate place to keep the list in order.
For example, insert( [2, 4, 6, 8], 5 ) should return [2, 4,
5, 6, 8].
- Write a Haskell function called insertSort that takes
one parameter (a list of numbers). The function should return the
list that would result from sorting the list using insertion sort.
Note: the insert function should be useful here, as would an
auxiliary function.
- Write a Haskell function called declist that takes one
parameter (a list of numbers). The function should return a list of
numbers with each number one less that the corresponding number in
the original list. For example, declist( [1, 2, 3, 4] )
should return [0, 1, 2, 3].
- Write a Haskell function called position that takes as
parameters an integer and a list of numbers. The function will
return the position of the integer in the list, where the first
position is 1. For example, position( 3, [1, 3, 4, 5] )
should return 2.
- Challenge: Modify problem 7 so that it returns the number 0 if
the integer is not in the list.
DePauw
University, Computer Science
Department, Spring 2005
Maintained by Brian
Howard (bhoward@depauw.edu
).
Last updated