Is there a way to convert symbol to string only if it is not already a string in lisp?
It should work like this:
(only-if-convertion 'ABC) => "ABC"
(only-if-convertion "ABC") => "ABC"
Use the function STRING.
CL-USER > (string "FOO")
"FOO"
CL-USER > (string 'FOO)
"FOO"
You can use the format function to do the conversion. Granted it's slower than the other options listed, but it can work on other data types, controls upcase/downcase, etc. So for development, or non-inner-loop portions of the code, this could be useful for you:
CL-USER>
(format nil "~a" "str")
"str"
CL-USER>
(format nil "~a" 'str)
"STR"
CL-USER>
(format nil "~(~a~)" 'str)
"str"
CL-USER>
(format nil "~(~a~)" "str")
"str"
CL-USER>
~
CL-USER> (defun symbol-or-string-to-string (x)
(typecase x
(symbol (symbol-name x))
(string x)
(otherwise (error "Wrong type"))))
SYMBOL-OR-STRING-TO-STRING
CL-USER> (symbol-or-string-to-string "foo")
"foo"
CL-USER> (symbol-or-string-to-string 'foo)
"FOO"
CL-USER> (symbol-or-string-to-string #())
; Evaluation aborted.
CL-USER>
But the idea of converting it repetitively sounds odd. Can you show why are you needing to do it?
Related
I have two strings of the same length which differ in exactly one character and I want a string of all the characters which are equal. So basically something like this which evaluates to a string instead of a list of characters:
(loop for a across "abcd"
for b across "abce"
when (char= a b) collect a)
Although performance isn't an issue here, I found it cumbersome to have a (coerce ... 'string) around it.
So I came up with something like
(loop with result = ""
for a across "abcd"
for b across "abce"
when (char= a b)
do (setf result (concatenate 'string result (string a)))
finally (return result))
which does the job but looks not very elegant to me.
(map 'string (lambda (a b) (when (char= a b) a)) "abcd" "abce")
looks nicer but is not working because NIL is not a character when aand bare not equal.
Is there a more elegant idiom to iterate over a string and get a string back?
(loop with result = ""
for a across "abcd"
for b across "abce"
when (char= a b)
do (setf result (concatenate 'string result (string a)))
finally (return result))
Repeated concatenate are not a that good idea for longer strings.
Alternatives:
Loop into a list and coercing to a string
CL-USER 3 > (loop for a across "abcd"
and b across "abce"
when (char= a b) collect a into list
finally (return (coerce list 'string)))
"abc"
Using stream and converting it to a string
CL-USER 4 > (with-output-to-string (*standard-output*)
(loop for a across "abcd"
and b across "abce"
when (char= a b) do (write-char a)))
"abc"
Using an adjustable string
CL-USER 5 > (loop with string = (make-array 0
:element-type 'character
:adjustable t
:fill-pointer 0)
for a across "abcd"
for b across "abce"
when (char= a b) do (vector-push-extend a string)
finally (return string))
"abc"
Another possibility is to use mismatch as in the comment of David Hodge:
CL-USER> (defun f(a b)
(let ((pos (mismatch a b)))
(concatenate 'string (subseq a 0 pos) (subseq a (1+ pos)))))
F
CL-USER> (f "abcdefg" "abcxefg")
"abcefg"
Use map to loop over multiple lists simultaneously
(map 'string #'(lambda (a b) (if (char= a b) a #\Rubout)) "abce" "abcd")
'string coerces resulting list into a string. #\Rubout get's coerced to a zero-length string. #\Backspace would even delete the last character.
Another way (it does not assume anything about elements position)
(defun only-matching (seq1 seq2)
(remove-if-not (lambda (c) (find c seq1)) seq2))
CL-USER> (only-matching "abcd" "abce")
"abc"
CL-USER> (only-matching "abdc" "abec")
"abc"`
or
(coerce
(intersection (coerce "abdc" 'list)
(coerce "abec" 'list))
'string)
which does not preserve order also
Note: remove-if-not is deprecated 'officially'.
For example I have (list "a" "1" "b" "2" "c" "3").
Now I want to turn this list into one "a1b2c3".
How do I do that?
Thank you.
(apply string-append (list "a" "1" "b" "2" "c" "3")) or (string-append* "" (list "a" "1" "b" "2" "c" "3")) should work. See: http://docs.racket-lang.org/reference/strings.html
If you wanted a procedure to do this you can just write (define (strings->string sts) (apply string-append sts))
Don't reinvent the wheel! in Racket, there exists one procedure specifically for this and its' called string-join:
(string-join '("a" "1" "b" "2" "c" "3") "")
=> "a1b2c3"
Quoting the documentation:
(string-join strs
[sep
#:before-first before-first
#:before-last before-last
#:after-last after-last]) → string?
strs : (listof string?)
sep : string? = " "
before-first : string? = ""
before-last : string? = sep
after-last : string? = ""
Appends the strings in strs, inserting sep between each pair of strings in strs. before-last, before-first, and after-last are analogous to the inputs of add-between: they specify an alternate separator between the last two strings, a prefix string, and a suffix string respectively.
For what it's worth, here are some implementations with and without a delimiter (i.e. a string that is inserted between each pair of strings, such as a space or a comma).
The functions fold and fold-right are from SRFI 1.
Using a string port is probably faster when concatenating very many or very long strings. Otherwise there's unlikely to be much speed difference.
Without delimiter argument
Using fold
(define (string-join strings)
(fold-right string-append "" strings))
Using recursion
(define (string-join strings)
(let loop ((strings strings) (so-far ""))
(if (null? strings)
so-far
(loop (cdr strings) (string-append so-far (car strings))))))
Using a string port
(define (string-join strings)
(parameterize ((current-output-port (open-output-string)))
(for-each write-string strings)
(get-output-string (current-output-port))))
With a delimiter argument
Using fold
(define (string-join strings delimiter)
(if (null? strings)
""
(fold (lambda (s so-far) (string-append so-far delimiter s))
(car strings)
(cdr strings))))
Using recursion
(define (string-join strings delimiter)
(if (null? strings)
""
(let loop ((strings (cdr strings)) (so-far (car strings)))
(if (null? strings)
so-far
(loop (cdr strings)
(string-append so-far delimiter (car strings)))))))
Using a string port
(define (string-join strings delimiter)
(if (null? strings)
""
(parameterize ((current-output-port (open-output-string)))
(write-string (car strings))
(for-each (lambda (s)
(write-string delimiter)
(write-string s))
(cdr strings))
(get-output-string (current-output-port)))))
I have to write a program that changes a string's vowels, consonants and other symbols into C, V respectively 0. I've done this but I wonder if there is a more efficient and elegant way to do it. Would appreciate input.
(defun string-to-list (string)
(loop for char across string collect char))
(defun is-vowel (char) (find char "aeiou" :test #'char-equal))
(defun is-consonant (char) (find char "bcdfghjklmnpqrstvwxyz" :test #'char-equal))
(defun letter-type (char)
(if (is-vowel char) "V"
(if (is-consonant char) "C"
"0")))
(defun analyze-word (word-string)
(loop for char across word-string collect (letter-type char)))
Moreover, I would like to make it a string, how could I do that? Should I define a function that would iterate through the list and make it a string or is it an easier way to do it?
(defun letter-type (char)
(cond ((find char "aeiou" :test #'char-equal) #\V)
((alpha-char-p char) #\C)
(t #\0)))
CL-USER> (map 'string #'letter-type "analyze-word")
"VCVCCCV0CVCC"
Just for the sake of the idea:
(defun multi-replace-if (sequence function &rest more-functions)
(map (type-of sequence)
(lambda (x)
(loop for f in (cons function more-functions)
for result = (funcall f x)
while (eql x result)
finally (return result)))
sequence))
(multi-replace-if "bcdfghjklmnpqrstvwxyz"
(lambda (x) (if (find x "aeiouy") #\v x))
(lambda (y) (declare (ignore y)) #\c))
"cccccccccccccccccccvc"
I want to add a character to a string, and get another string with the character added as a result.
This doesn't work:
(cons \a "abc")
Possible solutions, in order of preference:
Clojure core function
Clojure library function
Clojure user-defined (me!) function (such as (apply str (cons \a "abc")))
java.lang.String methods
Is there any category 1 solution before I roll-my-own?
Edit: this was a pretty dumb question. :(
How about:
(str "abc" \a)
This returns "abca" on my machine.
You can also use it for any number of strings/chars: (str "kl" \m "abc" \a \b).
You could use join from clojure.string:
(clojure.string/join [\a "abc"])
But for the simple use case you should really just use str, as #Dan Filimon suggests. join has the added benefit that you could put a separator between the joined strings, but without a separator it actually just applies str:
(defn ^String join
"Returns a string of all elements in coll, separated by
an optional separator. Like Perl's join."
{:added "1.2"}
([coll]
(apply str coll))
([separator [x & more]]
(loop [sb (StringBuilder. (str x))
more more
sep (str separator)]
(if more
(recur (-> sb (.append sep) (.append (str (first more))))
(next more)
sep)
(str sb)))))
I want to print floats in a good looking way. Specifically I want to print two numbers after the decimal point, but only if these numbers are not zero.
This works if the number is not an even integer:
(let ((f 1.240))
(format t "~,2F" f))
--> 1.24
But if the number is an integer I get this:
(let ((f 1240))
(format t "~,2F" f))
-->1240.00
Is there some elegant way to do this, or do I have to check the number of decimal points manually before printing out?
I don't think this is possible with standard format directives. You could write a custom format function:
(defun my-f (stream arg &optional colon at digits)
(declare (ignore colon at))
(prin1 (cond ((= (round arg) arg) (round arg))
(digits (float (/ (round (* arg (expt 10 digits)))
(expt 10 digits))))
(t arg))
stream))
And use it like this:
CL-USER> (format t "~/my-f/" 1)
1
NIL
CL-USER> (format t "~/my-f/" 1.0)
1
NIL
CL-USER> (format t "~/my-f/" pi)
3.141592653589793D0
NIL
CL-USER> (format t "~/my-f/" 1.5)
1.5
NIL
CL-USER> (format t "~2/my-f/" 1)
1
NIL
CL-USER> (format t "~2/my-f/" 1.0)
1
NIL
CL-USER> (format t "~2/my-f/" pi)
3.14
NIL
CL-USER> (format t "~2/my-f/" 1.5)
1.5
NIL
You could use a FORMAT conditional expression:
(let ((f 1240))
(format t "~:[~,2f~;~d~]" (integerp f) f))
--> 1240