Arc Forumnew | comments | leaders | submitlogin
Return value of pr and prn
3 points by gus_massa 5911 days ago | 9 comments
I think that pr and prn should return the last argument, no the first argument. Because some similar instructions work in this way. For example:

  arc> (do 1 2 3 4 5)
  5
  arc> ((fn () 1 2 3 4 5))
  5
  arc> (let tmp 'nil 1 2 3 4 5)
  5
  arc> (prn 1 2 3 4 5)
  12345
  1
  arc> (pr 1 2 3 4 5)
  123451


4 points by bOR_ 5910 days ago | link

Wouldn't it be more useful it prn and pr not only have their side-effect of printing, but also return the sideeffect (i.e., return the string they just printed)

-----

2 points by stefano 5910 days ago | link

It doesn't make much difference. pr and prn are usually used for side effects only.

-----

6 points by Darmani 5910 days ago | link

Actually, especially since Arc lacks a debugger, I find it very convenient to be able to just place a pr: or pr. in front of some symbol to get an expression printed out without the behavior changing.

-----

1 point by bOR_ 5910 days ago | link

Wouldn't it be even more beneficial to return the whole, rather than just the first (so that it prints out the whole content of the symbol)?

-----

2 points by gus_massa 5910 days ago | link

I agree. I think that (pr 1) should return 1.

The problem is with (pr 1 2). Should it return 1 or 2?

-----

1 point by stefano 5909 days ago | link

It could return the list (1 2), but this would mean to cons up a new list every time we use 'pr just to throw it away. Multiple return values would solve the problem. Why not add them to the language?

-----

1 point by bOR_ 5909 days ago | link

at least in agent-based models, printing is not the thing that is happening so often that consing up a new list is prohibitive.

How much more expensive is cons compared to multiple return values? I remember there being a long discussion against mrvs.

-----

2 points by stefano 5908 days ago | link

> How much more expensive is cons compared to multiple return values?

It depends on the GC used and on the implementation. mrvs are usually pushed on the stack, so there is no GC overhead. When I develop applications with Lisp, I notice that most of the time reducing the amount of garbage speeds up the program noticeably. It's not only a problem of consing, it's also a problem of collecting the garbage. More garabage, more collection cycles.

-----

1 point by xrchz 5899 days ago | link

do pr or prn create a string? it might be less expensive to return that string if it is created

-----