chansey97
2021-7-25 08:37:23

Is there a built-in treemap procedure in Racket? For example: (treemap even? '(1 2 (3 4) 5)) => '(#f #t (#f #t) #f)



chansey97
2021-7-25 08:39:02

Thanks


sorawee
2021-7-25 08:44:15

Oh, I missed the word “built-in”. The answer seems to be no then.


chansey97
2021-7-25 08:44:50

Thanks. I dont use that lib. I have to write by myself.


sorawee
2021-7-25 08:45:02

yeah, it’s easy to write enough


chansey97
2021-7-25 08:45:48

Yes. I just think that procedure is very common.


chansey97
2021-7-25 09:25:41

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


sorawee
2021-7-25 09:26:16

Perhaps ~s


sorawee
2021-7-25 09:27:05

E.g., (~s '(1 2 3 #t)) => "(1 2 3 #t)"


sorawee
2021-7-25 09:27:39

(~s '(1 2 3 #b101)) will return "(1 2 3 5)" though


chansey97
2021-7-25 09:28:49

What about reverse?


sorawee
2021-7-25 09:29:04

meaning given a string, get an s-exp?


chansey97
2021-7-25 09:29:20

no


chansey97
2021-7-25 09:29:31

given a file (which is an s-exp), read it as a string.


sorawee
2021-7-25 09:30:20

I usually use with-input-from-file


sorawee
2021-7-25 09:30:37

actually, scratch that


sorawee
2021-7-25 09:30:49

for your case, file->string might be easier


chansey97
2021-7-25 09:31:26

Sorry. if I only have port?


sorawee
2021-7-25 09:31:33

port->string


chansey97
2021-7-25 09:32:52

Thanks. I usually read from port, but read always convert a string to sexp.


sorawee
2021-7-25 09:33:17

there are other “read mode”, like read-line, read-char, read-string


soegaard2
2021-7-25 09:34:25

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.


chansey97
2021-7-25 09:35:28

> E.g., (~s '(1 2 3 #t)) => "(1 2 3 #t)" Which procedure is shorthand for ~s?


sorawee
2021-7-25 09:35:49

~s is ~s, but I think it stands for “s-exp”


chansey97
2021-7-25 09:36:06

~s is very google unfriendly.


sorawee
2021-7-25 09:36:08

It’s a mode in format. E.g., (format "~s" 1)


chansey97
2021-7-25 09:36:30

Thanks!


sorawee
2021-7-25 09:36:31

You should use the search functionality in https://docs.racket-lang.org/



chansey97
2021-7-25 09:36:53

Nice!


soegaard2
2021-7-25 09:37:07

Why are you interested in conserving the original form of the s-expression ?


chansey97
2021-7-25 09:39:59

Because the smtlib result from z3.exe has some symbols I want to conserve.


chansey97
2021-7-25 09:40:19

For example, #x123 #b101


sorawee
2021-7-25 09:40:57

oh… you are working with Z3


sorawee
2021-7-25 09:43:43

You should take a look at other libraries that deal with Z3. Such as Rosette


sorawee
2021-7-25 09:43:59

For example, when reading stuff from Z3 process, you can use readtable to help with reading


soegaard2
2021-7-25 09:44:00

I am beginning to think, writing your own read-sexp-to-string using the primitive read-char , peek-char etc. is the best approach.



sorawee
2021-7-25 09:49:13

sorawee
2021-7-25 09:49:39

Emina’s code is extremely well-documented, so you should be able to follow it


chansey97
2021-7-25 09:49:53

Thanks.


soegaard2
2021-7-25 09:54:46

@sorawee That’s a nice trick! (changing the readtable)


sarna.dev
2021-7-25 16:10:35

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


sorawee
2021-7-25 16:11:32

Some people put a- in front of it


sorawee
2021-7-25 16:11:39

a-dog


sorawee
2021-7-25 16:13:00

I’ve also seen the- prefix


sarna.dev
2021-7-25 16:14:28

ah that’ll work, thanks


ben.knoble
2021-7-25 16:15:16

Tried this a couple of times til it broke constructors/match/etc.—ended up using a or the prefixes


laurent.orseau
2021-7-25 16:59:27

I also use a compact version of the struct name sometimes, if it’s intuitive enough. Depends where it sits in the code though


laurent.orseau
2021-7-25 16:59:41

For test, I tend to add a number


soegaard2
2021-7-25 17:01:10

Btw - one can use #:extra-constructor-name to make an extra constructor name. Such as make-foo.


ben.knoble
2021-7-25 18:43:20

But you still lose the match-expander if you shadow the struct name, right @soegaard2?


soegaard2
2021-7-25 18:43:50

Yes.


notjack
2021-7-25 19:16:10

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)


notjack
2021-7-25 19:17:56

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



sarna.dev
2021-7-25 19:35:13

but then chess could be the main struct, with a field called board :eyes: