9 Replies - 7257 Views - Last Post: 22 March 2010 - 02:54 PM

#1 Guest_curioComp*


Reputation:

how does haskell pass parameters?

Posted 17 March 2010 - 07:37 AM

hi,
how does haskell pass parameters to a function and how does it return them? is it by reference or by value?
thank you!
Is This A Good Question/Topic? 0

Replies To: how does haskell pass parameters?

#2 s243a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: how does haskell pass parameters?

Posted 17 March 2010 - 09:20 PM

View PostcurioComp, on 17 March 2010 - 06:37 AM, said:

hi,
how does haskell pass parameters to a function and how does it return them? is it by reference or by value?
thank you!


I am new to haskell but my guess is I don't think it would because it is a lazy language and tries to avoid evaluating things as much as possible. However, keep in mind that variables once declared do not change (at least to my understanding). Therefore, if you impose strict evaluation of an input argument and only make a small change to the input data structure then the likely result I presume would be creating a close copy of the old variable. Therefore, I presume that under some circumstances you would still get the inefficiencies of pass by value. I guess it would be up to the compiler to avoid duplication of data as much as possible but I also presume that how practical this is will depend on how you structure your code.
Was This Post Helpful? 0
  • +
  • -

#3 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 50
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: how does haskell pass parameters?

Posted 18 March 2010 - 04:19 PM

Haskell is a purely functional language which means there are no side effects to functions. Parameters will always be passed by value.
Was This Post Helpful? 0
  • +
  • -

#4 s243a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: how does haskell pass parameters?

Posted 18 March 2010 - 04:28 PM

View Postprogramble, on 18 March 2010 - 03:19 PM, said:

Haskell is a purely functional language which means there are no side effects to functions. Parameters will always be passed by value.


I think this is misleading. Say, you passed a tree to a binary search function. The search function would not need to copy the entire tree structure to find the value. It would just need to keep asking for the next branch until it finally makes it to the last leaf. There is no reason, to copy all the leaves and since Haskell is lazy I don't think it would. Maybe in an abstract sense you can think of it as pass by value but since it is lazy I don't that it is pass by value in the traditional sense.
Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 50
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: how does haskell pass parameters?

Posted 19 March 2010 - 12:54 PM

Yes, but a function that adds a leaf to a binary tree would not modify the original tree, it would return a new one.
Was This Post Helpful? 0
  • +
  • -

#6 s243a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: how does haskell pass parameters?

Posted 19 March 2010 - 03:24 PM

View Postprogramble, on 19 March 2010 - 11:54 AM, said:

Yes, but a function that adds a leaf to a binary tree would not modify the original tree, it would return a new one.


I presume this would be the case but I wonder if there are any possible optimizations that are implemented in any of the Haskell Compilers/Interpreters which will allow us to avoid doing this.

So think about lazy evaluation for a bit:

getLeaf(addLeaff(Tree))

I am sure it possible to compose these operations without creating a new tree. The only question I suppose is how is it implemented.

addLeaf only really needs to be called if get leaf searches the exact node node where the leaf is added.
Was This Post Helpful? 0
  • +
  • -

#7 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 50
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: how does haskell pass parameters?

Posted 20 March 2010 - 12:08 PM

The fact that a new tree is created is part of the language. Whether or not the new tree takes up the same amount of memory as the old one is part of the compiler and shouldn't affect the developer.
Was This Post Helpful? 0
  • +
  • -

#8 s243a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: how does haskell pass parameters?

Posted 20 March 2010 - 12:35 PM

View Postprogramble, on 20 March 2010 - 11:08 AM, said:

The fact that a new tree is created is part of the language. Whether or not the new tree takes up the same amount of memory as the old one is part of the compiler and shouldn't affect the developer.


In my opinion it depends. Generally the trend is programing is to put the emphasis on how easily a task can be programed and therefore these considerations aren't relevant. However, in cases where speed is important such optimization concerns become relevant. For instance in a numeric package where large matrices are passed between functions it becomes inefficient to copy an entire matrix just to set a single variable. It is therefore important in such cases when deciding if the language is suitable to understand how effective the compiler is at eliminating unnecessary copies.

This is also an important consideration when writing code. For instance if you call an external function (outside Haskell) which requires the entire matrix to be read, then you are forcing Haskell to recreate the entire matrix instead of giving it a chance to try and optimize operations. If alternatively you had a way of only transferring the elements which changed then there could be potential for optimization.
Was This Post Helpful? 0
  • +
  • -

#9 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: how does haskell pass parameters?

Posted 21 March 2010 - 06:28 AM

There is a slight overhead to creating a new tree, but not one that is overly significant. I'm sure dons would be happy to talk to you about optimization.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Guest*


Reputation:

Re: how does haskell pass parameters?

Posted 22 March 2010 - 02:54 PM

It technically passes parameters by reference in most cases (some compiler optimisations aside), but as it is pure, you need never worry about this. Semantically the code above can be said to generate a new tree, but the cost is not that of generating a whole new tree, as (at least the popular) Haskell compilers are capable of re-using parts of old data structures, as they are guaranteed to remain unmodified.
Was This Post Helpful? 1

Page 1 of 1