
Hello, I don't want to write kludgy Haskell code! typedef struct blah { int val1; union { int val2; struct { int val3; int val4; } } } C_type; question: in Haskell, can I embed definition of the "union" inside of the C typedef, i.e. recursion definition? Or must I have a separate definition for the "union" which I "instantiate" inside the Haskell "typedef", i.e. Haskell "data"? Kind regards, Vasili

Yes, you must write them seperately as something like data A = A Int B data B = B1 Int | B2 Int Int one of the many wonders of Haskell -- it encourages you to split up your code into nice small chunks. Bob On 30 May 2008, at 08:46, Galchin, Vasili wrote:
Hello,
I don't want to write kludgy Haskell code!
typedef struct blah { int val1;
union {
int val2;
struct {
int val3;
int val4; } } } C_type;
question: in Haskell, can I embed definition of the "union" inside of the C typedef, i.e. recursion definition? Or must I have a separate definition for the "union" which I "instantiate" inside the Haskell "typedef", i.e. Haskell "data"?
Kind regards, Vasili _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 29 May 2008, at 11:46 PM, Galchin, Vasili wrote:
Hello,
I don't want to write kludgy Haskell code!
typedef struct blah { int val1;
union {
int val2;
struct {
int val3;
int val4; } } } C_type;
question: in Haskell, can I embed definition of the "union" inside of the C typedef, i.e. recursion definition? Or must I have a separate definition for the "union" which I "instantiate" inside the Haskell "typedef", i.e. Haskell "data"?
Assuming all of these are semantic, you can say newtype HS_type = HS_type (Int, Either Int (Int, Int)) But you lose named fields. It's hard to give other advice without some idea of /why/ you want to do something like this. jcc

compactness in writing and also namespace pollution .. ;^)
Vasili
On Fri, May 30, 2008 at 2:12 AM, Jonathan Cast
On 29 May 2008, at 11:46 PM, Galchin, Vasili wrote:
Hello,
I don't want to write kludgy Haskell code!
typedef struct blah { int val1;
union {
int val2;
struct {
int val3;
int val4; } } } C_type;
question: in Haskell, can I embed definition of the "union" inside of the C typedef, i.e. recursion definition? Or must I have a separate definition for the "union" which I "instantiate" inside the Haskell "typedef", i.e. Haskell "data"?
Assuming all of these are semantic, you can say
newtype HS_type = HS_type (Int, Either Int (Int, Int))
But you lose named fields.
It's hard to give other advice without some idea of /why/ you want to do something like this.
jcc

On 30 May 2008, at 12:29 AM, Galchin, Vasili wrote:
compactness in writing and also namespace pollution .. ;^)
I know what the advantages of C's notation are. But getting the best notation out of Haskell generally doesn't happen by trying to make your code look like C. So the general answer to questions of the form `C can do x; can Haskell' is `No'. Don't do it like in C. It won't be idiomatic, it won't be elegant, and neither of us will like the results. Understand the features of Haskell that make your program compact, point-free (how Haskell handles namespace pollution), and elegant and use those. This will require a different factoring of your program vs. C. Sorry, but that's life. jcc

my knowledge of point-free is from category theory. in what sense is Haskell
point-free handle namespace pollution?
Kind regards, Vasili
On Fri, May 30, 2008 at 3:23 AM, Jonathan Cast
On 30 May 2008, at 12:29 AM, Galchin, Vasili wrote:
compactness in writing and also namespace pollution .. ;^)
I know what the advantages of C's notation are. But getting the best notation out of Haskell generally doesn't happen by trying to make your code look like C.
So the general answer to questions of the form `C can do x; can Haskell' is `No'. Don't do it like in C. It won't be idiomatic, it won't be elegant, and neither of us will like the results. Understand the features of Haskell that make your program compact, point-free (how Haskell handles namespace pollution), and elegant and use those. This will require a different factoring of your program vs. C. Sorry, but that's life.
jcc

Galchin, Vasili wrote:
my knowledge of point-free is from category theory. in what sense is Haskell point-free handle namespace pollution?
In the sense that you can write e.g. f = g . h instead of f x = g (h x) thereby avoiding the need to give a name the argument to f. Cheers Ben BTW, A: No. Q: Should I include quotations after my reply?

Galchin, Vasili wrote:
typedef struct blah { int val1;
union {
int val2;
struct {
int val3;
int val4; } } } C_type;
question: in Haskell, can I embed definition of the "union" inside of the C typedef, i.e. recursion definition? Or must I have a separate definition for the "union" which I "instantiate" inside the Haskell "typedef", i.e. Haskell "data"?
No. Each definition of a data type must occur at the top-level. Also, Haskell has only a very weak record system, a deficit that is generally agreed to be one of Haskell's greatest weaknesses. Like jcc, I'd also be interested what concrete problem prompted your question. While I have often found it annoying that in Haskell e.g. record labels are global in scope, I have never encountered a situation where I wanted to have lexically nested data type declarations. Cheers Ben
participants (4)
-
Ben Franksen
-
Galchin, Vasili
-
Jonathan Cast
-
Thomas Davie