Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "!!" mean in haskell?

Tags:

haskell

There are two functions written in the Haskell wiki website:

Function 1

fib = (map fib' [0 ..] !!)
    where
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

Function 2

fib x = map fib' [0 ..] !! x
    where
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

What does the "!!" mean?

like image 386
user3239558 Avatar asked Feb 24 '14 04:02

user3239558


People also ask

What does exclamation mark mean in Haskell?

It's a strictness declaration. Basically, it means that it must be evaluated to what's called "weak head normal form" when the data structure value is created.

What does -> mean in Haskell?

(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.

What do () mean in Haskell?

It's called the "unit type". You can think of it as Haskell's equivalent of void , in some respects. It's a type that has only one value (also () ). Follow this answer to receive notifications. answered Oct 13, 2015 at 20:57.

What is at symbol in Haskell?

The @ Symbol is used to both give a name to a parameter and match that parameter against a pattern that follows the @ . It's not specific to lists and can also be used with other data structures.


2 Answers

This is actually more difficult to read then it would seem at first as operators in haskell are more generic then in other languages.

The first thing that we are all thinking of telling you is to go look this up yourself. If you do not already know about hoogle this is the time to become familiar with it. You can ask it either to tell you what a function does by name or (and this is even more cool) you can give it the type of a function and it can offer suggestions on which function implement that type.

Here is what hoogle tells you about this function (operator):

(!!) :: [a] -> Int -> a

List index (subscript) operator, starting from 0. It is an 
instance of the more general genericIndex, which takes an index     
of any integral type.

Let us assume that you need help reading this. The first line tell us that (!!) is a function that takes a list of things ([a]) and an Int then gives you back one of the thing in the list (a). The descriptions tells you what it does. It will give you the element of the list indexed by the Int. So, xs !! i works like xs[i] would in Java, C or Ruby.

Now we need to talk about how operators work in haskell. I'm not going to give you the whole thing here, but I will at least let you know that there is something more here then what you would encounter in other programming languages. Operators "always" take two arguments and return something (a -> b -> c). You can use them just like a normal function:

add x y
(+) x y -- same as above

But, by default, you can also use them between expression (the word for this is 'infix'). You can also make a normal function work like an operator with backtics:

x + y
x `add` y -- same as above

What makes the first code example you gave (especially for new haskell coders) is that the !! operator is used as a function rather then in a typical operator (infix) position. Let me add some binding so it is clearer:

-- return the ith Fibonacci number
fib :: Int -> Int  -- (actually more general than this but do't worry about it) 
fib i = fibs !! i
    where
      fibs :: [Int]
      fibs = map fib' [0 ..]

      fib' :: Int -> Int
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

You can now work your way back to example 1. Make sure you understand what map fib' [0 ..] means.

I'm sorry your question got down-voted because if you understood what was going on the answer would have been easy to look up, but if you don't know about operators as the exist in haskell it is very hard to mentally parse the code above.

like image 111
John F. Miller Avatar answered Oct 06 '22 00:10

John F. Miller


(!!) :: [a] -> Int -> a

List index (subscript) operator, starting from 0. It is an instance of the more general genericIndex, which takes an index of any integral type.

See here: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#g:16

like image 36
GEMISIS Avatar answered Oct 05 '22 22:10

GEMISIS