
I am totally new to Haskell, so maybe this is a stupid question. Various languages have pointers (or references), for good reason. Haskell can at least partly do without them (they are only existing internally somehow). My question is: Does Haskell principally not need pointers (i.e. in case of 2 data structures needing to reference an other very large data structure) or is this a design flaw or have a overlooked something? -------------------------------------------------- HAYES TECHNOLOGIES Software Speed Optimization Bryan Hayes Managing Director Mannheimer Str. 8 D-67098 Bad Dürkheim Deutschland / Germany Tel. +49 / (0)6322 / 94 950 - 68 Fax +49 / (0)6322 / 94 950 - 69 Mobile Tel. +49 / (0)163 / 6111867 E-Mail: mailto:bryan.hayes@hayestechnologies.com Web-Site: http://www.hayestechnologies.com

Hello,
"Bryan" == Bryan Hayes <(Hayes Technologies)"
> writes:
Bryan> My question is: Bryan> Does Haskell principally not need pointers (i.e. in case of 2 Bryan> data structures needing to reference an other very large data Bryan> structure) or is this a design flaw or have a overlooked Bryan> something? if you hold two variables to a data structure, the structure is represented only once. This is established by a graph structure of data, instead of just a tree structure. If you modify one copy, only the reachable parts between the handle (variable) and the position where the change has been made, are copied. All common substructures that are not affected by the change remain shared. Memory is allocated and released automatically. However, if you prefer a more state-based way of programming, you can deal with pointers in a so-called "monad", but if you need pointers for no other reason than efficiency, you should first try to use efficient programming techniques and data structures in Haskell. Programming in Haskell is completely different from imperative programming but it'll be worth it from the perspective of programming productivity. Just finding a way to fit imperative style in the syntax of Haskell will not give you the benefit you may expect from Haskell. Have a look at the book by Simon Thompson: "Haskell: The Craft of Functional Programming". It explains the methodology of programming in Haskell very well. Cheers -- Christoph Herrmann

At 9:30 AM -0600 12/7/01, Bryan Hayes (Hayes Technologies) wrote:
I am totally new to Haskell, so maybe this is a stupid question. Various languages have pointers (or references), for good reason. Haskell can at least partly do without them (they are only existing internally somehow). My question is: Does Haskell principally not need pointers (i.e. in case of 2 data structures needing to reference an other very large data structure) or is this a design flaw or have a overlooked something?
In Haskell, you can arrange for a large data structure to be shared by giving it a name, and then using the name wherever you'd use a pointer in some other language. For example, in
t = [0..] -- could be quite large
b = 3 : t
c = 5 : t
the lists b and c share list t. Of course, these lists' implementations are full of pointers, but there's never a need for them to appear explicitly in Haskell programs. ------------------------------------------------------------------ Hamilton Richards, PhD Department of Computer Sciences Senior Lecturer Mail Code C0500 512-471-9525 The University of Texas at Austin Taylor Hall 5.138 Austin, Texas 78712-1188 ham@cs.utexas.edu hrichrds@swbell.net ------------------------------------------------------------------

"Bryan Hayes (Hayes Technologies)" wrote:
I am totally new to Haskell, so maybe this is a stupid question. Various languages have pointers (or references), for good reason. Haskell can at least partly do without them (they are only existing internally somehow). My question is: Does Haskell principally not need pointers (i.e. in case of 2 data structures needing to reference an other very large data structure) or is this a design flaw or have a overlooked something?
All Haskell compilers use pointers internally. The idea is that because Haskell is referentially transparent and side effect free, you can overwrite a function application with its result. For example, let x = [1..1000] in foo (A x) (B x) Will internally have "x" pointing to the function application [1..1000], when this function application is evaluated it will overwrite the memory cell "x" points to with the result. So eventually it will look like this: +---+---+ +---+---+ | A | x | | B | x | +---+---+ +---+---+ | | +---+-------+ | V +---+---+------+ | : | 1 | tail |---> The list 2..1000 +---+---+------+ Where each block is a memory cell and each arrow is a pointer. A book that describes this a lot better is: Simon Peyton-Jones. The implementation of functional programming languages. Prentice-Hall, 1987 Jan
participants (4)
-
Bryan Hayes (Hayes Technologies)
-
Ch. A. Herrmann
-
Hamilton Richards
-
Jan Kort