asp5
asp5

Reputation: 371

Haskell: get memory address of a list

is there a way to get the address of data element (say a list element) in Haskell.

combineLists :: [a] -> [a] -> [a]

combineLists [] y = y
combineLists (x:xs) y = x : combineLists xs y

*Main> let x=[1,23, 12, 45]
*Main> x
[1,23,12,45]
*Main> let y =[90, 56, 78]
*Main> y
[90,56,78]
*Main> let z = combineLists x y
*Main> z
[1,23,12,45,90,56,78]

Now would z be constructed completely by copying elements from x and y (internal haskell representation) or

would z be something like: z = [ [copy of all elements from x] y]

I wanted to see if &y == &z[4] (z[4] = 90).

Also is there a way to dump the internal representation using something similar to ctypes in Python.

Thanks.

Upvotes: 3

Views: 411

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170839

You can use StableName or reallyUnsafePointerEquality# (note the name and don't use in real programs; you'll also need MagicHash extension to call it) to check whether two expressions refer to the same object. See What advantages do StableNames have over reallyUnsafePtrEquality#, and vice versa? for the differences.

Upvotes: 4

Related Questions