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

  1. Write a function called filterEven that takes a list of Ints and returns all members of the list that are even. Hint even is built in.

  2. Write a function called filterPositive that takes a list of Ints and returns all members of the list that are positive.

  3. Write a function called filterVowels that takes a String and returns all members of the String that are vowels. Extra Write a similar one called filterConsonants

  4. Write a function called filterBiggerN that takes a list of Ints an Int n that returns all members of the list that are bigger than that n. Extra rewrite your filterPositive

Mapping like functions

  1. Write a function called doubleList that takes a list of Ints and returns a list with all elementts doubled.

  2. Write a function called lengthList that takes a list of Stringss and returns a list of the length of each string.

  3. Write a function called squareList that takes a list of Ints and returns a list with all elements squared.

  4. Write a function called multNList that takes a list of Ints and returns a list with all elements multipiled by n.

  5. Write a function called multNList that takes a list of Ints and returns a list with all elements converted to a String. Hint use the function show.

  6. Write a function called firstChars that takes a list of Strings and returns a list that contains just the first character in each sting. Hint use the function head.

Both filter and mapping

  1. 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.

  2. Write a function that takes a list of Int, adds 1 to all numbers, then returns all numbers that are now bigger than 0.

  3. 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

Updated: