String prepend: Difference between revisions

Emacs Lisp: Actually solve the task
(Added solution for Action!)
(Emacs Lisp: Actually solve the task)
Line 655:
 
=={{header|Emacs Lisp}}==
 
===version 1===
While strings in Emacs Lisp are mutable, they're fixed size. Therefore the <code>concat</code> function creates a new string and the existing string must be referenced twice:
<lang Emacs Lisp>
 
(defun glue (str1 str2)
<lang Lisp>(defvar str "bar")
(setq str (concat str1"foo" str2str) )
</lang>
str ;=> "foobar"</lang>
===version 2===
 
<lang Emacs Lisp>
This can be hidden by using a macro such as <code>cl-callf2</code> which expands into the above code:
(defun glue (str1 str2)
 
(format "%s%s" str1 str2) )
{{libheader|cl-lib}}
</lang>
<lang Lisp>(require 'cl-lib)
<b>Eval:</b>
 
<lang Emacs Lisp>
(setqdefvar str "World!bar")
(setqcl-callf2 str (glueconcat "Hello, foo" str) )
str ;=> "foobar"</lang>
(insert str)
 
</lang>
Buffers can be thought of as expandable strings:
<b>Output:</b>
 
<pre>
<lang Lisp>(let ((buf (get-buffer-create "*foo*")))
Hello, World!
(with-current-buffer buf
</pre>
(insert "bar"))
(with-current-buffer buf
(goto-char (point-min))
(insert str"foo")
(buffer-string)))
;; => "foobar"</lang>
 
=={{header|Erlang}}==
Anonymous user