Arc Forumnew | comments | leaders | submitlogin
2 points by ryantmulligan 6142 days ago | link | parent

String interpolation.

  (let var "world" 
    (prn "hello #{var}"))


1 point by fallintothis 6142 days ago | link

Why string interpolation when the functions take any number of parameters?

  (let var "world"
    (prn "hello " var))
Or, if you want the entire string, this + tostring (as mentioned here: http://arclanguage.org/item?id=202).

  (tostring
    (let var "world"
      (prn "hello, " var))) 
prn used alone would have a return value of simply "hello, ". With tostring, the return value is "hello, world\n".

Though it might be worth noting that in the source file, arc.arc, there looks to be the beginnings of a printf-like function:

  (let argsym (uniq)
  
    (def parse-format (str)
      (rev (accum a
             (with (chars nil  i -1)
               (w/instring s str
                 (whilet c (readc s)
                   (case c 
                     #\# (do (a (coerce (rev chars) 'string))
                             (nil! chars)
                             (a (read s)))
                     #\~ (do (a (coerce (rev chars) 'string))
                             (nil! chars)
                             (readc s)
                             (a (list argsym (++ i))))
                         (push c chars))))
                (when chars
                  (a (coerce (rev chars) 'string)))))))
    
    (mac prf (str . args)
      `(let ,argsym (list ,@args)
         (pr ,@(parse-format str))))
  )
So that you could say, apparently:

  (let var "world"
    (prf "hello, ~" var))
Granted, the hello world example isn't very illuminating as to the benefits of using prf over the regular pr / prn. But, hey, the option's there in some base form (after all, the code's rather experimental at this stage).

-----

1 point by ryantmulligan 6142 days ago | link

Why does prn only return the result of its first argument?

  arc> (prn "hello, " "world")
  hello, world
  "hello, "
Can anyone think of when this is useful at all?

-----

2 points by eds 6140 days ago | link

This is exactly how print, et al work under CL.

I suspect it is a debugging thing. Since pr and prn return their first argument verbatim you can put them in the middle of working code to find what the return value of something is, without breaking anything. As a trivial example:

(+ (prn 2) 3) ; prints 2 and returns 5

Maybe you wouldn't need it if you had a fancy debugging suite, but it can be useful if you are debugging manually.

-----

1 point by bogomipz 6142 days ago | link

I think the reason is that returning the concatenation would be very expensive. It would basically require a temporary write stream just to put together the return value. In the majority of cases, the return value is probably not even used.

To get the effect you want, simply concatenate as a separate step:

  (prn (string "hello, " "world"))

-----

1 point by akkartik 6142 days ago | link

But why not return the last element?

-----

5 points by akkartik 6142 days ago | link

No, there's a better way. tostring is #$%#ing brilliant.

-----