Live data from Hacker News

Should function arguments be reassignable or mutable?

mortoray.com

11–20 of 28 posts

Re: Should function arguments be reassignable or mutable?

#11
post #7

> If we look to a language like Haskell we see that reassigning variables, in general, is frowned upon (is it even possible?) This is perfectly valid Haskell: main = do let a = 7 print a let a = 9 print a but you'll get a warning for it: tmp.hs:4:7: warning: This binding for ‘a’ shadows the existing binding bound at tmp.hs:2:7

Why would they allow this? Is there any legitimate case where doing this makes sense? Seems very risky to me. In most languages I know you would expect that the "a" variables are the same.

Re: Should function arguments be reassignable or mutable?

#13
post #6
post #2

I feel the author isn't going far enough- make all variables immutable by default unless explicitly declared otherwise, and even then highly discouraged. Where mutable variables are needed, I prefer to hide that logic inside a method that exists solely to do that dirty business on an abstract level, not specific to my business logic. That let's me test the 'dirty' mutable method in an isolated place and removes a who…

Agreed. Erlang is another language with single assignment, i.e. every variable assignment target must have a new name. It's incredibly easy to see what calculations and conditions affect the final outcome.

How do you do a for loop in Erlang or something that sums up a value ?

Re: Should function arguments be reassignable or mutable?

#14
post #11
post #7

> If we look to a language like Haskell we see that reassigning variables, in general, is frowned upon (is it even possible?) This is perfectly valid Haskell: main = do let a = 7 print a let a = 9 print a but you'll get a warning for it: tmp.hs:4:7: warning: This binding for ‘a’ shadows the existing binding bound at tmp.hs:2:7

Why would they allow this? Is there any legitimate case where doing this makes sense? Seems very risky to me. In most languages I know you would expect that the "a" variables are the same.

In the past, I've used it in order to ensure the variable has a legitimate value. So, something like this (pseudo code):

    fn foo(a):
      a = a |> trim |> (default "chocolate")
      mix a "milk"

Well, in this contrived example, I wouldn't reassign a, just tack on `|> (mix "milk")` but in a complex example where you're reusing `a` in several places, it can make sense and is nicer than having to declare a bunch of variables just to hold the sanitized values of arguments.

Re: Should function arguments be reassignable or mutable?

#15
post #13
post #6

Earlier quoted context omitted.

Agreed. Erlang is another language with single assignment, i.e. every variable assignment target must have a new name. It's incredibly easy to see what calculations and conditions affect the final outcome.

How do you do a for loop in Erlang or something that sums up a value ?

[deleted]

Re: Should function arguments be reassignable or mutable?

#16
post #13
post #6

Earlier quoted context omitted.

Agreed. Erlang is another language with single assignment, i.e. every variable assignment target must have a new name. It's incredibly easy to see what calculations and conditions affect the final outcome.

How do you do a for loop in Erlang or something that sums up a value ?

You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions.

Here is an example of using a recursive function in Erlang to sum up a factorial.

  fac(1) ->
      1;
  fac(N) ->
      N * fac(N - 1).

Re: Should function arguments be reassignable or mutable?

#17
I'm having a hard time understanding why they should be treated any differently than normal variables. I suppose the way that I view parameters is as regular variables who are initialized to values provided by the caller. Is there any argument against default mutability which still makes sense when viewing parameters from this perspective?

Re: Should function arguments be reassignable or mutable?

#18
post #16
post #13

Earlier quoted context omitted.

How do you do a for loop in Erlang or something that sums up a value ?

You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions. Here is an example of using a recursive function in Erlang to sum up a factorial. fac(1) -> 1; fac(N) -> N * fac(N - 1).

Does Erlang handle the stack better? In most languages this would blow up if N got big enough.

Re: Should function arguments be reassignable or mutable?

#19
post #11

Earlier quoted context omitted.

Why would they allow this? Is there any legitimate case where doing this makes sense? Seems very risky to me. In most languages I know you would expect that the "a" variables are the same.

In the past, I've used it in order to ensure the variable has a legitimate value. So, something like this (pseudo code): fn foo(a): a = a |> trim |> (default "chocolate") mix a "milk" Well, in this contrived example, I wouldn't reassign a, just tack on `|> (mix "milk")` but in a complex example where you're reusing `a` in several places, it can make sense and is nicer than having to declare a bunch of variables just…

Looks like you are using it as a reassignable function argument. Makes sense.

Re: Should function arguments be reassignable or mutable?

#20
post #18
post #16

Earlier quoted context omitted.

You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions. Here is an example of using a recursive function in Erlang to sum up a factorial. fac(1) -> 1; fac(N) -> N * fac(N - 1).

Does Erlang handle the stack better? In most languages this would blow up if N got big enough.

Yes. Erlang has tail recursion which helps prevent that http://learnyousomeerlang.com/recursion
Post reply on HN