Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (:) do in Haskell? [duplicate]

Tags:

haskell

I've tried looking it up in hoogle and other various haskell dictionaries, but I can't find it. I was under the impression that it prepends, but I'm starting to see it in ways I haven't before and I've started second guessing myself.

For example, this is one of the questions that I don't understand:

(3 points) Fill in the blank with a pattern such that fun1 [(5,6),(7,8)] returns 5 and fun1 [(10,20),(30,40),(50,60)] returns 10:

and the answer is apparently:

         ((y,_):_)
  fun1 _____________ = y

But I am so confused by this. I understand that the underscores mean that you don't really care about what the types of those are, but I don't understand what the (:) does in this answer.

like image 910
Mdomin45 Avatar asked Apr 01 '14 11:04

Mdomin45


People also ask

How do I find duplicates in Haskell?

if it is just about knowing if the list contains duplicates you can use the nub function in Data. List like so: hasDup l = nub l == l (nub removes all duplicates... so this will be true only if there are no duplicates in the list.)

What does () mean in Haskell?

() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .

What is cons operator in Haskell?

The : operator is known as the "cons" operator and is used to prepend a head element to a list. So [] is a list and x:[] is prepending x to the empty list making a the list [x] . If you then cons y:[x] you end up with the list [y, x] which is the same as y:x:[] .


1 Answers

While the other answers correctly explain what : is they don't quite answer the question - in the answer you have in your question : isn't used as a function, but as a constructor to pattern match on. fun (x:xs) = x means "if the argument is of the format (x:xs) give me the x". Pattern matching is used to "pull apart" complex types based on their constructors in Haskell.

In particular, since : is a list constructor you can pull apart lists with :

(conceptually list is defined as data [] a = [] | (:) a [a], although you're not gonna get this to compile because it's builtin syntax).

A non list example: We could define a datatype data F a b = A a | B b. This would create a type F that's parameterized with two types a and b and two constructors A and B with the types a -> F a b and b -> F a b respectively.

You could then write functions that use pattern matching to get at the contained values, like

isA (A _) = True -- this value was constructed with A, so it is an A
isA (B _) = False -- this value was constructed with B so it is not an A

or

getA (A a) = a -- this value was constructed with A so we can get an a out of it
getA (B _) = undefined -- ohps! We can't get an a back here cause we don't have one!
like image 198
Cubic Avatar answered Oct 03 '22 22:10

Cubic