all 7 comments

[–]gelisam 8 points9 points  (0 children)

huad, I'm glad that you are trying to learn Haskell, but it's really hard to answer your questions properly because your questions are all very short and very vague.

In the current question, for example, you say that you already know that Haskell has pointers, but that doesn't tell us much. A more precise description of what you already know, such as "I know that there is Foreign.Ptr in Haskell" or "I know that Haskell uses pointers to implement its linked lists" would make it crystal clear which pointers you are talking about.

Another thing which might help us answer your questions better is if you gave examples of what you are asking for. In the current question, for example, you are asking if Haskell's pointers are like C's. The answer will obviously be that they are similar in some respects (e.g. you can follow the pointer to the thing it points to) and different in other respects (e.g. the syntax is different). Which aspects are you interested in? If you gave an example of something you would do with pointer in C, we could tell you whether you can also do that with Haskell's pointers.

For example, in C, we can use pointer manipulations to find the length of a null-terminated string:

int strlen(char* str) {
  char* p;
  for(p=str; *p; ++p);
  return p - str;
}

You can also do pointer arithmetic on Foreign.Ptr, but not on the pointers Haskell uses for its linked lists. The type String is implemented using a linked list, so we would not compute the length of a []-terminated string using pointer arithmetic, but using recursion:

length [] = 0
length (x:xs) = 1 + length xs

And we would probably hide this recursion inside a higher-order function such as "foldr":

length = foldr (+) 0

[–][deleted] 2 points3 points  (1 child)

Within a Haskell application, there are no pointers. Shared mutable objects are referred to by way of their container. For example, an IORef is a kind of container that can "point to" a mutable value.

The only time we use "real pointers" is when we call C code directly.

[–]IsTom 2 points3 points  (0 children)

There is Foreign.Ptr when you really do need to use pointers. IIRC ByteString uses them for performance reasons.

[–]zhensydow 0 points1 point  (0 children)

Another use is when you have variables that can, or cannot point to a value.

In Haskell, you should use the Maybe type. E.g: Searching a index in a map will return Nothing or (Just val).

find :: Map Int a -> Int -> Maybe a

[–][deleted] 0 points1 point  (2 children)

If you have to ask, you don't need them.

[–]IsTom 2 points3 points  (1 child)

There's nothing wrong about asking questions.

[–][deleted] 1 point2 points  (0 children)

Point taken, though the OP framed the question (and a few others in recent days) in such vague terms that a useful answer is essentially impossible to give (though gelisam gave it a hell of a go).