Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell data type function parameter

Tags:

types

haskell

What is the significance of the parenthesis in a function definition in Haskell with respect to the data type of parameters.

For example:

doStuff Name -> Age -> String
doStuff (NameConstr a) (AgeConstr b) = "Nom: " ++ a ++ ", age: " ++ b

with the following defined somewhere beforehand:

data Name = NameConstr String
data Age = AgeConstr Integer

Could the function parameters a and b be captured in a way that negates the need for parenthesis here?

FYI, I'm working through:

  • http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/#type-construction
  • http://learnyouahaskell.com/types-and-typeclasses ,

and I just can't seem grasp this finer detail yet.

like image 432
patmanpato Avatar asked Dec 15 '22 11:12

patmanpato


1 Answers

Without parentheses, the function would be deemed to have four parameters. I can't think of a counterexample where omitting brackets would lead to ambiguity, though.

If you want, you can redefine your types as follows:

data Name = NameConstr { getName :: String  }
data Age  = AgeConstr  { getAge  :: Integer }

so that your function can become:

doStuff n a = "Nom: " ++ getName n ++ ", age: " ++ show (getAge a)

(fixed the last part; a is an Integer and cannot be concatenated to a string)

like image 126
Koterpillar Avatar answered Dec 31 '22 03:12

Koterpillar