Haskell CW II.3a
Goals:
- Range notation
[a,b..c]to make lists of integers. - Apply a function to everything in a list by using the “list comprehension”.
- Filter out some elements from an existing list using a list comprehension.
- Use “brute force”, checking all possibilities for the ones that satisfy some condition.
Micro-projects
-
A list of the first 1000 integers.
-
A list of the first 1000 perfect cubes.
-
Set the variable
a3to be the sum of the first 100 perfect squares. -
The type signature for
a3, the previous answer.. -
A list of all of the perfect squares between 90 and 1919.
-
The type signature for a list of integers.
-
The product of the numbers in the list of perfect squares above.
-
A function
sqList lower upperto give all of the perfect squares betweenlowerandupper, inclusive.check_8 = (sqList 16 81 == [16,25,36,49,64,81]) -
The type signature for the function
sqList. -
Use your function to get the first 17 perfect squares between 1500 and 4500.
-
Write a new function sq25 that will return the first 25 perfect squares between
lowerandupper. (If there are not 25, the function will just return however many there actually are, instead of giving some error.) -
A list with every third lowercase letter, starting at ‘a’ then ’d’. (Hint: letters work the same as numbers.)
-
A string with the last half of the alphabet (beginning with ’n’).
-
Write a function that takes in a list and produces a list of ordered pairs
(item, more), giving the item and how many more items after it are in the list.
Basic functions
- The remainder when 921013 is divided by 31.
- The number of complete groups of 17 candies that can be formed from a box containing with 23148 hard candies.
- Find five to the twelfth power.
- Find seven to the 707 power.
- A curiosity: the integer number of times that 3 goes into (-17), and how much is “left over”.
- Know/learn how to get a positive and negative “left over” amount in the previous question.
Medium functions
Write the signature for each.
-
Write a function
mySmallthat takes in four integer inputs and returns the smallest. -
Create a function
myStartsthat takes in two lists and returns a tuple with the first element from each list. -
Write
myMixthat takes in two lists and gives a tuple with the first element from the first list and the last element from the second list.myMix [1,2,3] [10,20,30] == (1,30) -
Create
dublrthat takes in one list and returns a new list consisting of the same information forward then backward.dublr [1,2,3] == [1,2,3,3,2,1]
Step up
-
Write a function
sqLast25 lower upperthat will return the last 25 perfect squares between lower and upper (inclusive).sqLast25 :: Int -> Int -> [Int]
Obscure
- What is the successor of the character ‘Z’?
- How about the predecessor of the character ‘A’?
Notes
- There is a small difference between
divandquotalthough both are the same for positive numbers.