3

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?

Rn2dy
  • 4,112
  • 12
  • 43
  • 59
  • 2
    You might profit from one of the books for people new to Haskell. I like [Learn You a Haskell For Great Good](http://learnyouahaskell.com/) and [Real World Haskell](http://book.realworldhaskell.org/). Both are free to read online. – rampion Jul 03 '10 at 04:49
  • I'm a bit puzzled. Your functionX takes a list of strings but returns a single function. Or maybe you actually mean: "functionX :: String -> Int -> Int" Then you can apply it to a list of strings and integers using zipWith. By the way, your brackets are not needed; "->" is right-associative. – Paul Johnson Jul 03 '10 at 07:30
  • See also http://stackoverflow.com/questions/3136338/uses-for-haskell-id-function – Don Stewart Apr 19 '11 at 03:26

1 Answers1

7

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
  • 87,131
  • 49
  • 199
  • 315
  • So the type of the identity function is [Integer] -> [Integer] ? – Rn2dy Jul 03 '10 at 04:32
  • 1
    I may be remembering incorrectly, but i think haskell defaults to generic types – Cogwheel Jul 03 '10 at 04:38
  • 3
    @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 Jul 03 '10 at 04:42
  • all right, the thing is, how can I handle the call "functionX []" – Rn2dy Jul 03 '10 at 04:42
  • Then just handle the other case separately : `function (x:xs) = ...` – rampion Jul 03 '10 at 04:47
  • 3
    In Haskell these types are not called 'generic' but rather 'polymorphic'. In the context of Haskell, 'generic' means something else entirely. – Martijn Jul 05 '10 at 22:22