String prepend: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
(Emacs Lisp: Actually solve the task) |
||
Line 655: | Line 655: | ||
=={{header|Emacs Lisp}}== |
=={{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") |
|||
(concat |
(setq str (concat "foo" str)) |
||
</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> |
|||
( |
(defvar str "bar") |
||
( |
(cl-callf2 concat "foo" str) |
||
str ;=> "foobar"</lang> |
|||
⚫ | |||
</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)) |
|||
⚫ | |||
(buffer-string))) |
|||
;; => "foobar"</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |