Maps, Folds, browsing Haskell with Hoogle

in #programming7 years ago

hoogle.png
Hoogle is a search engine for the Haskell programming language, which lets you browse data types and function names within the broad array of standard libraries. If you're not sure about the name of a process you're thinking of, but you know the type of data that it operates on, Hoogle might have an answer for you.

For example, lets say we wanted a function that adds each number in a list to an accumulator, and returns the resulting accumulation, we first have to understand the type signature of our function.

(what) :: (a -> b -> a) -> a -> [b] -> a

What this says is as follows. We are looking for a function, returning a value of type a, which takes three arguments:

  1. Its first is a function of two arguments, a & b, and this must return some value of type a.
  2. Its second argument is the initial value of the accumulator, for simple summations this argument will most often be 0.
  3. Its third parameter is the list from which to pick values.

For example, let's say we wanted to sum up the values of the list [1,2,3,4,5], how could we do this?
input: what (+) 0 [1..5]
output: 15
This works because (+) has the a less general type signature than what is required to be the first parameter to our mystery function:
(+) :: Num a => a -> a -> a
So (+) can be the first argument of "what." But what exactly is "what?"

If you look up what's type signature on Hoogle:
what = foldl = foldr

There you have it! We were thinking of a process called "folding," and we were able to find that by knowing about the parameters, and types of parameters, for such process. What do you think is the type signature for "map?" Map is a computing process that is common across all languages. It is much like a fold, but unlike a fold, which returns an accumulated value given a list, a map produces a new list, which resembles the old list, after a function of one argument is applied to each item in the list. See for yourself!

Sort:  

Congratulations @juicyasf! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @juicyasf! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @juicyasf! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of comments

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!