Haskell CW II.3a
Tiny pieces to help you get better at programming in Haskell.
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.
Micro-projects
- A list of the first 1000 integers.
- A list of the first 1000 perfect cubes.
- Set the variable
a3
to 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
to give all of the perfect squares betweenlower
andupper
. - 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
lower
andupper
. (If there are not 25, the function will return however many there are.) - 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 whole number of times that 23148 is divisible by 17.
- Find five to the twelfth power.
- Find seven to the 707 power.
Medium functions
Write the signature for each.
-
Write a function
mySmall
that takes in four integer inputs and returns the smallest. -
Create a function
myStarts
that takes in two lists and returns a tuple with the first element from each list. -
Write
myMix
that 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
dublr
that 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 upper
that 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
div
andquot
although both are the same for positive numbers.