8 Replies - 8056 Views - Last Post: 09 June 2011 - 11:11 AM

#1 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

"id" in haskell?

Posted 07 June 2011 - 02:02 PM

so im trying to pick up haskell and im reading about the "do" keyword at the moment. some code was shown as an example for simple file IO but there is 1 detail that i don't understand.

import System.Environment (getArgs)

interactWith function inputFile outputFile = do
    input <- readFile inputFile
    writeFile outputFile (function input)

main = mainWith myFunction
    where mainWith function = do
              args <- getArgs
              case args of
                  [input,output] -> interactWith function input output
                  _              -> putStrLn "error: exactly two arguments needed"
          myFunction = id -- <- here what is this?



so what is id? where was it defined? it's a function from what i can tell but i still have no idea what it is exactly.

the source of the code can be found here

here is all they said about it

Quote

When we want to test a function that cannot talk to the outside world, we simply replace the name id in the code above with the name of the function we want to test. Whatever our function does, it will need to have the type String -> String: in other words, it must accept a string, and return a string.


Is This A Good Question/Topic? 0
  • +

Replies To: "id" in haskell?

#2 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: "id" in haskell?

Posted 07 June 2011 - 02:05 PM

'id' is the "identity" function. The identity function takes one argument and returns that argument.

id foo == foo
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: "id" in haskell?

Posted 07 June 2011 - 02:32 PM

then that makes perfect sense in this context(meaning the code makes sense now) but what's the point of it? why not just use the argument it's self?

This post has been edited by ishkabible: 07 June 2011 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 I X Code X 1   User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: "id" in haskell?

Posted 07 June 2011 - 04:39 PM

Identity functions in Haskell have the form:

 id :: b -> b 


In other words, The identity function I(x) = x takes a single input, x, and immediately returns x (i.e. the identity does nothing with its input).

I've read (not actually ever used) that id functions can and are used primarily with higher-order functions. Higher order functions are simply functions that take other functions as parameters. For example,

 Prelude> :t id
id :: a -> a

Prelude> :t foldr (.) id
foldr (.) id :: [a -> a] -> a -> a

; This is how we would add a list of numbers

Prelude> foldr (.) id (map (+) [1..5]) 0
15




I found this example here

With basic Haskell knowledge you should be able to follow that. Basically, in a nut shell, id returns itself and for practical use it is used in higher-order functions.
Was This Post Helpful? 1
  • +
  • -

#5 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: "id" in haskell?

Posted 07 June 2011 - 06:30 PM

ya i have heard a lot about higher-order programing, passing functions to change functionality and what not. in C++ we use virtual functions for that sort of thing ;)

that dose make more sense, it would be used in where you need to pass a function but don't want anything more than to just pass the value it's self.

i assume there is an idiom to follow in here, right? if so what idiom(s) should i be aware of? when and where should i make my functions higher order?
Was This Post Helpful? 0
  • +
  • -

#6 I X Code X 1   User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: "id" in haskell?

Posted 07 June 2011 - 07:05 PM

Edit: Try this link as well, has some good Haskell examples of Higher order functions


I'm not sure if there really is one set definition of when to use higher-order functions. From my own experience I use them when I want to perhaps show some abstraction in my code. That way I am not trying to clutter everything into one function. They really help to make code more readable, managable, and reusable. My rule of thumb (though this holds no ground towards anything): if you think the section of code might be 'general' enough to use somewhere else, break it up into another function so that it can be reused. Not just used in one big function.

"Functions are the wonderful and powerful building blocks of computer programs. Functions allow you to break code down into simpler, more manageable steps. They also allow you to break programs into reusable parts -- parts that are both reusable within the program and in other programs as well."


Also, remember: Higher order functions are a function that takes a function as an argument, or returns a function as a result; this latter use is just as important as functions that take functional arguments.

This post has been edited by I X Code X 1: 07 June 2011 - 07:21 PM

Was This Post Helpful? 1
  • +
  • -

#7 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: "id" in haskell?

Posted 07 June 2011 - 08:00 PM

thanks ill keep that in mind, im reading the link right now as well. man you've been helpful in functional forums, great job :) i hope to see more of you in the forums.
Was This Post Helpful? 0
  • +
  • -

#8 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: "id" in haskell?

Posted 08 June 2011 - 09:34 PM

Indeed, id is useful in fairly specific cases. Let's build upon our friend's explanation (which is correct) and work through a few examples. I'm going to use Clojure here because I don't have Haskell on this computer, but the examples will be simple and easy to understand. Note that in Clojure, the id function is called identity.

Let's remind ourselves of what identity does:

user=> (doc identity)
-------------------------
clojure.core/identity
([x])
  Returns its argument.
nil



The documentation on the identity function says it returns it's argument. That is precisely what it does. We can implement this ourselves easily:

user=> (defn id [x] x)
#'user/id



And now we have another identity function. In Haskell, that would be id x = x.

Let's try out the identity function:

user=> (identity "foo")
"foo"
user=> (identity 10)
10



Now, the important part: why is it useful? Let's come up with a scenario. Somebody has written a function called play-with-numbers or playWithNumbers, as it would be written in Haskell. This function would take a number and a function. It would multiply the number by 10 and then apply the function to it. Let's try that out:

user=> (defn play-with-numbers [n f] (f (* n 10)))
#'user/play-with-numbers
user=> (play-with-numbers 10 str)
"100"



Above, we defined our play-with-numbers function and then used it. We passed it the number 10 and the str function. What the str function does is convert it's arguments to strings and concatenates them. In this case, our number is multiplied by 10, resulting in 100, and then str is called on it, coercing it to a string which is then returned.

The cool thing about our function is not what it does, but what it is capable of doing. It is totally extensible. You can pass it a function and you can make the function do any number of good things. This is the power of higher order functions.

Now, what if you just want the function to do what it itself does: multiple a number by 10. What if you want to multiply your number by 10, but don't actually have anything else to do? What do you do then?

user=> (play-with-numbers 10 identity)
100



We passed identity as the function's argument, so after the number is multiplied it is simply returned. This is a good example of where identity can be useful. In Clojure specifically, identity serves another fantastic purpose.

In Clojure, anything that isn't nil or false is considered true. Therefore, we can do things like this:

user=> (filter identity [1 2 nil "hi" {:foo "bar"} nil "hehe" false true])
(1 2 "hi" {:foo "bar"} "hehe" true)



filter is a function that takes a function and a sequence and then applies that function to each element in the sequence, removing any elements that the function returns a non-true value on. Therefore, if we pass identity, filter will filter out all nil and false values. Pretty cool, huh?
Was This Post Helpful? 2
  • +
  • -

#9 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: "id" in haskell?

Posted 09 June 2011 - 11:11 AM

very cool, i feel like im starting to get the hang of this functional programing thing :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1