tags:

views:

126

answers:

1

I have a function with type like this:

functionX :: [String] -> ([Integer] -> [Integer])

It is kind of like a mapping function that maps a specific String to a function with type as so. Because I need to handle the call functionX [], which I think this call should return something called identity function, or whatever, how can I write it?

+3  A: 

id is predefined as the identity function in haskell. It has type id :: a -> a.

If you wanted, you could define your own easily:

myIdentityFunction :: a -> a
myIdentityFunction a = a
rampion
So the type of the identity function is [Integer] -> [Integer] ?
baboonWorksFine
I may be remembering incorrectly, but i think haskell defaults to generic types
Cogwheel - Matthew Orlando
@baboonWorksFine: Cogwheel's right. The identity function's return type is parameterized by its input type. Since it doesn't do anything with the input other than return it, it places no constraints on the input's type. In Haskell, lowercase letters in a type signature (like `id :: a -> a`) indicates that those symbols stand for a type parameter, rather than a concrete type like `String` or `Integer`.
rampion
all right, the thing is, how can I handle the call "functionX []"
baboonWorksFine
`functionX [] = id`
rampion
or, `functionX [] is = is`
rampion
I am a very green one
baboonWorksFine
Then just handle the other case separately : `function (x:xs) = ...`
rampion
In Haskell these types are not called 'generic' but rather 'polymorphic'. In the context of Haskell, 'generic' means something else entirely.
Martijn