Is there a built-in treemap procedure in Racket? For example: (treemap even? '(1 2 (3 4) 5)) => '(#f #t (#f #t) #f)
Thanks
Oh, I missed the word “built-in”. The answer seems to be no then.
Thanks. I dont use that lib. I have to write by myself.
yeah, it’s easy to write enough
Yes. I just think that procedure is very common.
Is there way to convert a datum (e.g. a list) to a string? For example: '(1 2 3 #t) => "(1 2 3 #t)"
and read a file (whose content is a s-exp) as a string? For example: read this file: a.txt (1 2 3 #b101)
to a string "(1 2 3 #b101)"
Perhaps ~s
E.g., (~s '(1 2 3 #t))
=> "(1 2 3 #t)"
(~s '(1 2 3 #b101))
will return "(1 2 3 5)"
though
What about reverse?
meaning given a string, get an s-exp?
no
given a file (which is an s-exp), read it as a string.
I usually use with-input-from-file
actually, scratch that
for your case, file->string
might be easier
Sorry. if I only have port?
port->string
Thanks. I usually read
from port, but read
always convert a string to sexp.
there are other “read mode”, like read-line
, read-char
, read-string
If the standard read
is used to read an s-expression it will turn "(1 2 3 #b101)"
into (1 2 3 5)
. If you really want some read function to return "(1 2 3 #b101)"
then my best suggestion is to use read-syntax
to read the s-expression. Look at the source location information, which contains information on where the s-expression began and ended in the file. Then reread the piece from the file using read-bytes
or read-string
.
> E.g., (~s '(1 2 3 #t))
=> "(1 2 3 #t)"
Which procedure is shorthand for ~s?
~s is ~s, but I think it stands for “s-exp”
~s is very google unfriendly.
It’s a mode in format
. E.g., (format "~s" 1)
Thanks!
Nice!
Why are you interested in conserving the original form of the s-expression ?
Because the smtlib result from z3.exe has some symbols I want to conserve.
For example, #x123 #b101
oh… you are working with Z3
You should take a look at other libraries that deal with Z3. Such as Rosette
For example, when reading stuff from Z3 process, you can use readtable
to help with reading
I am beginning to think, writing your own read-sexp-to-string
using the primitive read-char
, peek-char
etc. is the best approach.
When writing to Z3, see https://github.com/emina/rosette/blob/master/rosette/solver/smt/enc.rkt
When reading from Z3, in addition to that make-readtable
+ read
, see https://github.com/emina/rosette/blob/master/rosette/solver/smt/dec.rkt
Emina’s code is extremely well-documented, so you should be able to follow it
Thanks.
@sorawee That’s a nice trick! (changing the readtable)
how do you name instances of structs? whenever I have a struct foo
my instinct is to name the variable foo
, but it doesn’t really work in this case
Some people put a-
in front of it
a-dog
I’ve also seen the-
prefix
ah that’ll work, thanks
Tried this a couple of times til it broke constructors/match/etc.—ended up using a
or the
prefixes
I also use a compact version of the struct name sometimes, if it’s intuitive enough. Depends where it sits in the code though
For test, I tend to add a number
Btw - one can use #:extra-constructor-name
to make an extra constructor name. Such as make-foo
.
But you still lose the match-expander if you shadow the struct name, right @soegaard2?
Yes.
It works easier if your struct names are more than one word long. For example, if you name a struct chess-board
then you can use board
for local variables (which are more likely to have a small enough scope that the context makes it obvious what kind of board it is)
You can get the match expander back even if you’re providing the shadowing constructor with a contract. Use define-match-expander
combined with define-module-boundary-contract
but then chess
could be the main struct, with a field called board
:eyes: