Is there a shorthand syntax for list that allows symbols in Chicken Scheme? - chicken-scheme

When I write:
'(1 2 3)
I get a list:
(1 2 3)
When I write:
'some-symbol
I get:
some-symbol
When I write:
'('some-symbol)
I get:
((quote some-symbol))
I can of course write:
(list 'some-symbol)
and I get:
(some-symbol)
which is the desired output. Is it correct that I cannot quote a symbol in a list like:
'(some-symbol)
Is there some other shorthand for the list operator that I am missing?

Quote will quote the entire s-expression that follows. So, in that sense,
'(some-symbol)
will actually be a quoted list containing the symbol you're looking for.
The quote sign is a shorthand for (quote ...), so
'(some-symbol)
is equivalent to
(quote (some-symbol))

Related

How to convert an object to string in Emacs Lisp, quoting symbols so it can be read back?

I'd like to use a function similar to prin1-to-string, but which retains quotes for symbols and writes all objects in a way that can be sent back to eval. If I use prin1-to-string, it removes the quotes from symbols:
ELISP> (prin1-to-string 'a)
"a"
ELISP> (prin1-to-string '(2 a))
"(2 a)"
But the string "a" is not the form I passed to prin1-to-string. The function I want would work as this:
ELISP> (great-printing-function 'a)
"'a"
ELISP> (great-printing-function '(2 a))
"(2 'a)"
This is for handling history in a toy REPL I've been working on (different from IELM -- one that would be usable in a terminal) -- that's why I mentioned it will be sent back to eval.
Maybe there is some variable that would cause prin1-to-string to not unquote symbols? Or some other function? Otherwise, I'd probably need to code a tree-walker...

What does the ' represent in '+ in racket?

I have been banging around on google and drRacket trying to understand what the apostrophe ' before a procedure means in racket and how I could remove it. What I'm trying to do is take a + from inside a list i.e. '(+ 1 2). However, every time I do something like (first x) (where x is the list in the example) I receive '+ instead of just + (notice the apostrophe). How can I remove the apostrophe and what is its purpose?
The ' apostrophe, pronounced quote, mean that the stuff inside will be interpreted as data for an s-expression, not evaluated as code.
'x, 'hello, and '+ are all symbols, which are like strings with unique-identity properties. They contain only text, not "meaning", so the '+ symbol does not contain a reference to the + function.
If you use parentheses under a ' quote, it will create a list with all the elements also ' quoted. In other words, '(x y z) is equivalent to (list 'x 'y 'z). You can think of this as quote "distributing itself" over all the elements inside it.
In your case, '(+ 1 2) is equivalent to (list '+ '1 '2), which is the same as (list '+ 1 2) because numbers are already literal data.
In most cases, the best way to get rid of a ' quote is to not add one on the outside in the first place. Instead of '(+ 1 2) you could use list: (list + 1 2), or the more advanced forms ` quasiquote and , unquote: `(,+ 1 2). In either of these cases, the + never gets put under a quote in the first place. It never becomes a symbol like '+. The + outside of any quote has meaning as the addition function.
In other cases, you can't avoid having the symbol '+ because it comes from intrinsically textual data. In this case you can assign meaning to it with an interpreter. Somewhere in that interpreter you might want code like this
(match sym ['+ +] ['- -] ['* *] ['/ /] [_ (error "unrecognized symbol")])
Something is needed to assign meaning externally, because the symbol '+ does not have that meaning internally. You can either define the interpreter yourself or use an existing one such as eval, as long as all the meanings in the interpreter correspond exactly to what you intend.

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~}"

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 " ")

Converting input to list in prolog

I am doing something like this
read_line_to_codes(user_input,Li1),nl,
write(Li1),nl,
atom_codes(A,Li1),
write(A),nl
So I am getting this output-
|: 'hello',18,19,'Bye'
[39,104,101,108,108,111,39,44,49,56,44,49,57,44,39,66,121,101,39]
'hello',18,19,'Bye'
But I want a list having this-L=['hello',18,19,'Bye']
How can I do this?
In SWI-Prologyou can use atomic_list_concat/3 with the first argument (a list) uninstantiated, the second argument the separatorn (',' in your case) and the third argument the atom you already have (A). Note that this use of atomic_list_concat/3 is not portable
E.g.:
atomic_list_concat(L, ',', A)

Resources