Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

http://susam.in/blog/clumsy-pointers/

How can be solved different declarations puzzles with Haskell?

void x1(int *) A function which has int * argument and return type void.

void (*x2)(int *) Pointer to a function which has int * argument and return type void.

void (*x3())(int *) A function that returns a pointer to a function which has int * argument and return type void.

void (*x4(void (*)(int *)))(int *) A function which has a pointer to a function that has int * argument and return type void as argument, and returns a pointer to a function which has int * argument and return type void.

void (*x5(void (*[10])(int *)))(int *) A function which has an array of 10 pointers to functions that has int * argument and return type void as argument, and returns a pointer to a function which has int * argument and return type void.

void (*(*x6)(void (*[10])(int *)))(int *) A pointer to a function which has an array of 10 pointers to functions that has int * argument and return type void as argument, and returns a pointer to a function which has int * argument and return type void.

char (*(*x7())[])() Function returning pointer to array[] of pointer to function returning char.

char (*(*x8[3])())[5] Array[3] of pointer to function returning pointer to array[5] of char.

share|improve this question

closed as not a real question by GlenH7, Jalayn, gnat, Bill, Martijn Pieters May 16 '13 at 22:45

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Are those int* parameters meant to be arrays, out-parameters or in-out parameters? C uses the same type for all (well, in function signatures; no claim for elsewhere). –  Donal Fellows Nov 24 '11 at 11:17
    
The interpretation of int* is up to you. I propose a solution using Maybe, and Arrays. But instead of Maybe, [] also works. –  Zhen Nov 24 '11 at 12:33
1  
Err. Why did you just copy most of that blog post into a question? Are you asking how to solve the puzzle in the blog post in Haskell instead? –  Anna Lear Nov 27 '11 at 6:59
    
I asking in StackOverflow, because here we have vote system. –  Zhen Nov 27 '11 at 12:05
1  
@Zhen Firstly, this is not Stack Overflow. Secondly, what's your actual question? You copied someone's blog post with almost no explanation. –  Anna Lear Nov 27 '11 at 17:55

1 Answer 1

up vote 2 down vote accepted

A solution using Maybe as a pointer and Array for arrays.

import Data.Array

x1 :: Maybe Int -> ()
x1 a = ()

x2 :: Maybe (Maybe Int -> ())
x2 = Just (\a-> ())

x3 :: Maybe (Maybe Int -> ())
x3 = x2

x4 :: (Maybe (Maybe Int -> ())) -> Maybe (Maybe Int -> ())
x4 a = x2

x5 :: Array Int (Maybe (Maybe Int -> ())) -> Maybe (Maybe Int -> ())
x5 a = x2

x6 :: Maybe (Array Int (Maybe Int -> ()) -> Maybe (Maybe Int -> ()))
x6 = Just (\a -> x2)

x7 :: Maybe (Array Int (Maybe Char))
x7 = Just (listArray (1,1) [Just 'a'])

x8 :: Array Int (Maybe (Maybe (Array Int Char)))
x8 = listArray (1,3) (replicate 3 (Just (Just (listArray (1,5) "abcde"))))
share|improve this answer
2  
Notice that in Haskell, something(void) and something are actually the same type. –  FUZxxl Nov 24 '11 at 11:18
    
Right FUZxxl. The most tricky thing I notice is that a function without parameters returning a Char, and a Char is the same. –  Zhen Nov 24 '11 at 12:33
2  
Also note, that function returning () is totally useless in Haskell. When binding C functions, they usually need return type IO something and than something can meaningfully be (). –  Jan Hudec Nov 24 '11 at 13:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.