Declare a string in elisp and then inserting it in emacs lisp - elisp

I want to create i string and then insert it, but i get the error wrong argument char-or-stringp. What does it mean?
(defun test ()
(string "1 2 3")
)
(insert (test))

Your string call is incorrect. "1 2 3" is already a string. You seem to be looking for simply
(insert "1 2 3")
or if you want to demonstrate a function call,
(defun test ()
"1 2 3")
(insert (test))
The string form expects a sequence of characters, like this:
(string ?1 ? ?2 ? ?3)
=> "1 2 3"

Related

Splitting string by space and number Ruby

how do I split string by space and following specific number? My string looks like
PID55688.00976 1
PID66854.76557 2
PID88774.23455 3
PID66843.99754 1
PID66800.00764 3
I want to split string by space and number 3
Code
str.split(/\s3/) does not split anything.
This produces the expected output that OP has describe in the comments on #spickermann's answer:
string = <<-STRING
PID55688.00976 1
PID66854.76557 2
PID88774.23455 3
PID66843.99754 1
PID66800.00764 3
STRING
string.split(/ 3\n/).map{|substring| (substring+" 3").split(/\n/)}
but there's some typos in OP's expected answer, as there's no commas or quotes in the arrays. So I might have misinterpreted.
I would do something like this:
string = <<-STRING
PID55688.00976 1
PID66854.76557 2
PID88774.23455 3
PID66843.99754 1
PID66800.00764 3
STRING
string.scan(/(\w+\.\w+ \w+)\n/).flatten
#=> [["PID55688.00976 1"], ["PID66854.76557 2"], ["PID88774.23455 3"], ["PID66843.99754 1"], ["PID66800.00764 3"]]
Assuming
[["PID55688.00976 1", "PID66854.76557 2", "PID88774.23455 3"],
["PID66843.99754 1", "PID66800.00764 3"]]
is the desired return value, one could write:
string.split(/\r?\n/).slice_after { |s| s.end_with?(' 3') }.to_a
See Enumerable#slice_after and String#end_with?.

Use variable padding in iteration directive in FORMAT

Is there a way to do something like the following?
(format t "~{~va~}" '("aa" "bb" "cc") 4)
I need to iterate through a list. Each element of that list should be padded with a variable number of spaces (specified at runtime, so I cannot use "~4a").
Or more generally, is there a way to refer to a specific argument in the argument list of FORMAT?
By nesting format function, you can do what you want.
(format t (format nil "~~{~~~Aa~~}" 4) '("aa" "bb" "cc"))
;; returns: aa bb cc
Here the inner format directive:
The nil as first argument, format returns a string.
(format nil "~~{~~~Aa~~}" 4)
;; returns: "~{~4a~}" - and this is exactly what you want to give
;; to the outer `format` as second argument!
You can of course write a function for this:
(defun format-by-padding-over (lst padding)
(format t (format nil "~~{~~~Aa~~}" padding) lst))
And then:
(format-by-padding-over '("aa" "bb" "cc") 4)
;; aa bb cc
;; NIL
I learned this trick here from #Sylwester (many thanks!).
You could also interleave the list with repetitions of the padding:
(format t "~{~va~}"
(mapcan (lambda (element)
(list 4 element))
list))
You can build the format control string using nested format functions, but then you have to take care about escaping tildes. When working with regular expressions (using CL-PPCRE), one can define regular expressions using trees, like (:alternation #\\ #\*), which helps preventing bugs and headaches related to escaping special characters. The same can be done with format strings, using format-string-builder, available in Quicklisp:
(lambda (v)
(make-format-string `((:map () (:str ,v)))))
Returns a closure, which can be used to build format strings:
(funcall * 10)
=> "~{~10a~}"

car/cdr arguments are lists... right? [duplicate]

This question already has answers here:
What is the difference between quote and list?
(2 answers)
Closed 4 years ago.
I made a list of lists of strings called chatbot in Scheme.
Every list element in chatbot have strings. I'm trying to classify these strings using different lists, and these lists are all stored in a big list called chatbot. (sorry for redundance)
To make it clear, here is the code doing this:
(define greetings '("string 1"
"string 2"
"string 3"
"string 4"))
(define cheerUpPhrases '("string 5"
"string 6"))
(define congratsPhrases '("string 7"
"string 8))
(define didNotUnderstand '("string 8"
"string 9"
"string 10"))
(define chatbot '(greetings cheerUpPhrases congratsPhrases didNotUnderstand))
I really think this is okay. But later, in a function, I wanted to get "string 3" so I tried to do this:
(caddar chatbot)
and then got this error:
caddar: contract violation
expected: (cons/c (cons/c any/c (cons/c any/c pair?)) any/c)
given: '(greetings cheerUpPhrases congratsPhrases didNotUnderstand)
Not very sure of what that meant, I changed (caddar chatbot) into:
(third (car chatbot))
Finally, I got this error:
third: contract violation
expected: list?
given: 'greetings
Now, I understand (third) needs a list (actually pair) to work; and so are car/cdr and similar functions. Am I not giving it a list after all? I'm really confused now.
I'm just starting with Scheme and the functional paradigm, so I may be missing a basic thing. It would really help me if you could explain me what's going on.
Thanks in advance.
Close. the problem is your misuse of '.
You see, '(a b c) doesn't expand to something like (list a b c), but rather, it expands to something like (list 'a 'b 'c).
So, instead of inserting the lists greetings, cheerUpPhrases, congratsPhrases, and didNotUnderstand, you actually inserted them literally as symbols.
There are two easy ways to get around this. Either you can use quasiquote ` and unquote ,, or you can just use list directly. Rewriting your code as using list (and fixing a your broken "string 8" gives:
(define greetings '("string 1"
"string 2"
"string 3"
"string 4"))
(define cheerUpPhrases '("string 5"
"string 6"))
(define congratsPhrases '("string 7"
"string 8"))
(define didNotUnderstand '("string 8"
"string 9"
"string 10"))
(define chatbot (list greetings cheerUpPhrases congratsPhrases didNotUnderstand))
You can now see that the chatbot variable contains the actual list you were expecting:
> chatbot
'(("string 1" "string 2" "string 3" "string 4") ("string 5" "string 6") ("string 7" "string 8") ("string 8" "string 9" "string 10"))

let: bad syntax (not an identifier and expression for a binding) in: wordslist ###scheme

(define (most-common-word str)
(let (wordslist str-split str " ")))
I am trying to do a inner variable of lists of strings.
but I get the error "bad syntax".
I looked for answers here but the things I changed, didn't help.
str-split returns a list of strings with " " the separator.
thanks.
It should look like:
(let ([word-list <VALUE>]) <BODY>)
... which establishes a local binding from word-list to the value <VALUE>. This binding is effective only inside the <BODY> form enclosed by the let.
Now, in order to compute <VALUE>, you have to call str-split with the arguments you want (i.e. str and " "). The way you perform a function call is to wrap it in parenthesis (this is valid only in a context where the form is evaluated as an expression, not where parenthesis mean binding, for example). So <VALUE> should really be:
(str-split str " ")

string-set! not working in Racket

Following code is giving error:
(define s "test")
(string-set! s 0 #\T)
The error is:
string-set!: contract violation
expected: (and/c string? (not/c immutable?))
given: "test"
argument position: 1st
other arguments...:
How can I change a character of 's' using string-set! function? I think it has to be made "mutable". How can that be done?
That's because a string defined using the default reader is immutable; it's literally the first thing stated in the documentation. For example, to turn it into a mutable string:
(define s (string-append "test"))
(string-set! s 0 #\T)
It works as expected:
s
=> "Test"

Resources