Haskell 2
Hello, after 3 weeks of doing the same workshop over and over agin, today we will be finally be introducing new content.
Last time I think it was too long, so this time it will a bit more bit sized and mostly exercises beacuse I’m doing this the day before and I am too lazy to make content.
Map and Filter Functions
Filter like functions
-
Write a function called
filterEventhat takes a list ofIntsand returns all members of the list that are even. Hintevenis built in. -
Write a function called
filterPositivethat takes a list ofIntsand returns all members of the list that are positive. -
Write a function called
filterVowelsthat takes aStringand returns all members of the String that are vowels. Extra Write a similar one calledfilterConsonants -
Write a function called
filterBiggerNthat takes a list ofIntsanIntnthat returns all members of the list that are bigger than thatn. Extra rewrite yourfilterPositive
Mapping like functions
-
Write a function called
doubleListthat takes a list ofInts and returns a list with all elementts doubled. -
Write a function called
lengthListthat takes a list ofStringss and returns a list of the length of each string. -
Write a function called
squareListthat takes a list ofInts and returns a list with all elements squared. -
Write a function called
multNListthat takes a list ofInts and returns a list with all elements multipiled byn. -
Write a function called
multNListthat takes a list ofInts and returns a list with all elements converted to aString. Hint use the functionshow. -
Write a function called
firstCharsthat takes a list ofStrings and returns a list that contains just the first character in each sting. Hint use the functionhead.
Both filter and mapping
-
Write a function that takes a list of
Int, finds all numbers that are bigger than zero, and returns a new list of all numbers plus 1. -
Write a function that takes a list of
Int, adds 1 to all numbers, then returns all numbers that are now bigger than 0. -
Hard Write a function that takes a 2d matrix (a list of list of ints) and returns the transpose.
the map and filter functions
If you did everything right, you should been basically been writing teh same code every time.
Haskell has something called high order functions, which means functions take functions as arguments and return functions as arguments.
We’ve actually already been doing that with the power of currying, but I’ll explain that at a later time.
I’m tired of typing so I’m going to have chatgpt explain the rest to you.
Lambda functions, also known as anonymous functions, are functions that are defined without a name. They are useful for creating quick, small functions that aren’t needed elsewhere. In Haskell, lambda functions are often used in higher-order functions (like map, filter, foldr, etc.) where you need to pass a function as an argument.
Syntax of Lambda Functions in Haskell
The syntax for a lambda function in Haskell is:
Start with a backslash () — this represents the Greek letter lambda (λ) in lambda calculus. Next, list the function’s parameters separated by spaces. After the parameters, place an arrow (->). Finally, define the function body after the arrow.
\x -> x + 1
We can use lambda expressions with the new functions map and filter
map basically does a transformation of one list into another list
filter basically takes a list and selects elements of the list that have a certain property
incrementList :: [Int] -> [Int]
incrementList list = map (\x -> x + 1) list
filterNegatives :: [Int] -> [Int]
filterNegatives list = filter (\x -> x >= 0) list
Exercise Redo all the exercise using the new filter and map
Exercise implement your own version of map and filter, you might have to restrict it to just Int or String
** Generics
Ok, well, I guess this is a good lead in.
- Extra Resources
http://learnyouahaskell.com/chapters
https://en.wikibooks.org/wiki/Haskell