Using map in Haskell

For people new to functional programming map may be one of the first concepts that they struggle with. map is a function that takes two parameters: a function and a list of elements. The type signature of map is (a -> b) -> [a] -> [b]. The (a -> b) part is the function you pass to map, we will call it f. f takes one value and … Read more

What does Cons and :-: mean in Haskell?

I understand what most of this means apart from Cons. When I try :t Cons and :i Cons in ghci I get a not in scope error. You need to load the Haskell source file with the data declaration before you can have Cons in scope. Or, alternatively, you can enter that data line directly in GHCi. For serious code, it’s easier if you put it in … Read more

Haskell pattern matching – what is it?

In a nutshell, patterns are like defining piecewise functions in math. You can specify different function bodies for different arguments using patterns. When you call a function, the appropriate body is chosen by comparing the actual arguments with the various argument patterns. Read A Gentle Introduction to Haskell for more information. Compare: with the equivalent Haskell: Note … Read more

Understanding the functions elem and isInfixOf

Your problem is with the (** a) syntactic sugar. The thing is that (elem b) is just the partial application of elem, that is: However when we use back ticks to make elem infix, we get a special syntax for infix operators which works like this: So therefore, while So in the latter case your arguments are in the … Read more