How does one create a keyword symbol from a string? [duplicate] - common-lisp

This question already has answers here:
Common Lisp Programmatic Keyword
(6 answers)
Closed 8 years ago.
Making a symbol from a string is quite simple:
(intern "test") => test
I'm struggling to create keyword symbols for a plist. Looking for something like:
(XXXX "test") => :test
Note that (intern ":test") does not produce a keyword symbol but rather a symbol containing a colon (e.g. |:test|).
How can one correctly generate keyword symbols in Common Lisp? Thanks!

Keywords are normal symbols except that they live inside the KEYWORD package:
? (defvar x (intern "NEWKW" "KEYWORD"))
X
? x
:NEWKW
? (keywordp x)
T
See also here.

Related

In R, how to change working directory more easily? [duplicate]

This question already has answers here:
Escaping backslash (\) in string or paths in R
(4 answers)
Closed 1 year ago.
In how to change working directory more easily?
Currently, if we use 'setwd',we have to add many '\', sometimes it's boring
Is there any easier way for this ? (Just like Python can add 'r' )
setwd('C:\Users\Administrator\Desktop\myfolder') # can't work
setwd('C:\\Users\\Administrator\\Desktop\\myfolder') # can work,but havt to add many '\'
You could use r (for raw string) and add parenthesis:
> r"(C:\Users\Administrator\Desktop\myfolder)"
[1] "C:\\Users\\Administrator\\Desktop\\myfolder"
>
And now:
setwd(r"(C:\Users\Administrator\Desktop\myfolder)")
Or reading from clipboard automatically adds the extra slashes:
setwd(readClipboard())

How would I print out a list with spaces in between elements

So I'm trying to print out a list that looks a little bit something like this (setq lst (list '- '- '- '- '-)) and in the past I used the print command to print out the whole list, however, when printing the whole list there is parenthesis on each side which I do not want to see. I want to use something like (format t) to print every bit of my list and I have something like this set up.
(loop for item from 0 to 4
do (progn
(format t "~X" (nth item lst))
)
)
This code prints out the list perfectly fine like this, ----- but as a mentioned, I want it to print spaces between each element so that it is output like this - - - - -. I used the conditional "~X" because I looked up how to output spaces with the format command and you are apparently supposed to use "~X" but it does not work so if anybody knows how I could put spaces between elements that would be greatly appreciated.
Why not just use the features provided by format:
CL-USER> (defvar *my-list* '(- - - -))
*MY-LIST*
CL-USER> (format nil "~{~A~^ ~}" *my-list*)
"- - - -"
CL-USER> (format t "~{~A~^ ~}" *my-list*)
- - - -
NIL
Here the first call to format outputs to a string to show where the spaces are placed. ~{ is an iteration directive that takes a list as its argument. The directives between the opening ~{ and closing ~} are used repeatedly as a format string for the elements of the input list. The ~^ directive causes an early escape from the iteration context when there are no more arguments; this prevents a trailing space from being added.
The second call to format just outputs to *standard-output*.
Regarding your update, that you posted in the answers to your own post:
First of all, you should edit your post to show us that you found a solution, rather than having us look through all the answers to see how much progress you made on your initial problem.
As it was already mentioned in another answer, you can iterate through the elements of a list using format built-in syntax with ~{~} and ~^ (see the documentation !)
In your own solution, when you iterate over the list using loop, you can put a space at the end of the format string rather than calling format twice ...
You can use loop for <elt> in <list> rather than iterating with the indices, and calling nth at each step - which is slower, and also more verbose.
The loop ... do <stuff> already wraps the <stuff> in what we call an implicit progn, i.e. you do not need to wrap yourself all your instructions in a progn, the loop macro does that for you.
There also exists the macro dolist, which is (arguably) simpler to use in those case when you simply want to iterate over a list.
To be fair, it looks like you are a Common Lisp beginner. In this case, I suggest you read the excellent Practical Common Lisp book, which covers in details the loop macro, the format function, and a lot of basic principles. It is available for free online, and is often recommended to beginners, for good reasons !
Ok I came up with an ingenius solution to my problem which I definitely should've seen before.
(loop for item from 0 to 4
do (progn
(format t "~X" (nth item lst))
(format t " ")
)
)
I didn't realize I could print a space like that but it works perfectly fine. Sorry for wasting you all's time but hopefully someone else can see this if they are having a brain fart like me and thanks to everyone who tried to help.

Why "R help" does not work for some commands? [duplicate]

This question already has answers here:
How to get help in R?
(6 answers)
Closed 2 years ago.
I wanted to use "help in R" in order to see some information about some commands such as "for", "if", "while", "repeat" etc. But there is no information in "R help" regarding such commands. I would like to know why?
I use "R help" for the above commands like below:
?for
?while
?if
?repeat
R requires that keywords are used in syntactically valid form. The way R works, it expects that if for instance is followed by an expression in parentheses, and a body. ?if is not valid R syntax.
Conversely, ? is an operator that expects an identifier after it.
To make it valid, you should quote the if identifier in backticks. That way, R parses the expression as ? followed by an identifier, rather than ? followed by an incomplete if expression:
?`if`
Backtick-quoting is R’s way of saying: “hey, that thing between backticks is a valid identifier, even if it totally doesn’t look like one”. You could (but generally shouldn’t!) totally use it to use wonky variable names:
`name with spaces` = 2
message(`name with spaces` + 5)
# 7
This feature is more useful when applied to column names of externally imported data (which sometimes contains spaces or other invalid identifier characters), or when defining operators.

Handlebars - output a value inside curly braces [duplicate]

This question already has an answer here:
Simple way to escape curly braces?
(1 answer)
Closed 5 years ago.
My output needs to look somewhat like:
{foo} bar
The problem is the value foo comes from either a field or a helper. I could make the helper include the brackets {foo} but that doesn't seem a perfect solution. On the other hand if I try:
{{{foo_field}}} {{bar_field}}
That doesn't work as tripple braces also render the field in a slightly different manner.
Solution copied from Simple way to escape curly braces?
{ {{~foo_field~}} } {{bar_field}}

How to remove beginning-digits only in R [duplicate]

This question already has answers here:
Remove numbers at the beginning and end of a string
(3 answers)
Remove string from a vector in R
(4 answers)
Closed 5 years ago.
I have some strings with digits and alpha characters in them. Some of the digits are important, but the ones at the beginning of the string (and only these) are unimportant. This is due to a peculiarity in how email addresses are stored. So the best example is:
x<-'12345johndoe23#gmail.com'
Should be transformed to johndoe23#gmail.com
unfortunately there are no spaces. I have tried gsub('[[:digit:]]+', '', x) but this removes all numbers, not just the beginning-ones
Edit: I have found some solutions in other languages: Python: Remove numbers at the beginning of a string
As per my comment:
See regex in use here
^[[:digit:]]+
^ Asserts position at the start of the string
You can do this:
x<-'12345johndoe23#gmail.com'
gsub('^[[:digit:]]+', '', x) #added ^ as begin of string
Another regex is :
sub('^\\d+','',x)

Resources