joshibharathiramana
2020-11-8 14:04:53

The mnemonic I use to figure out what cxxxr does is a - access and d - drop first element respectively. So for instance, cddaar would be access-access-drop-drop (function composition is right to left!)


phanthero
2020-11-8 14:31:43

Does length take constant time for list ? Probably for vector, but not sure about implementation of list in particular


laurent.orseau
2020-11-8 14:50:29

It takes linear time (sadly)


laurent.orseau
2020-11-8 14:51:07

list? Takes amortised constant time though.


phanthero
2020-11-8 14:57:45

Ok I see. Should be easy enough to define a struct which wraps around a list to track length though


phanthero
2020-11-8 14:58:20

Is there an easy way to check this kind of runtime efficiency (is it in the docs somewhere?)


sorawee
2020-11-8 15:05:06

Time (and memory) complexity is one thing I wish Racket doc could be clearer about.


phanthero
2020-11-8 15:10:59

Hmmm, the implementation of Racket is public somewhere or the other right? Is there an easy way to lookup the corresponding code?


phanthero
2020-11-8 15:11:39

Since Racket is written in Racket things should be easy to understand for everyone too


sorawee
2020-11-8 15:13:23

Not for low level stuff like lists. Those are defined in Chez Scheme layer.


sorawee
2020-11-8 15:13:40

(and C code for the BC variant)


sorawee
2020-11-8 15:15:29

Here’s the very same question last month. You can follow the links for the source code: https://racket.slack.com/archives/C09L257PY/p1601916642152000


phanthero
2020-11-8 15:21:47

I have no idea which variant I’m using. I downloaded v7.3 off the official site, is that the CS version?

Edit: Ok so it seems from here: https://download.racket-lang.org/v7.3.html that I am still using BC as I am using 7.3


hoshom
2020-11-8 16:35:01

I’m trying to go from a small racket program that uses classes to a typed racket version of the program, but I can’t find a combination of types that works. Specifically, if I have a recipe% class, and an amount-mixin and unit-mixin , and each of these 3 have 1-argument init specifiers, how do I add types to both mixins such that (amount-mixin (unit-mixin recipe%)) works?


hoshom
2020-11-8 16:38:25

The plain racket version: http://pasterack.org/pastes/87386 The typed racket version (that raises an error): http://pasterack.org/pastes/44174


laurent.orseau
2020-11-8 16:41:32

In DrRacket, is the first line of the interaction window. On the command line, it’sracket -v


phanthero
2020-11-8 16:52:55

@laurent.orseau yes but this doesn’t tell you what the backend of racket is implemented with, only the version number

at least in my case since I’m using 7.3, not sure if this has changed in more recent versions


laurent.orseau
2020-11-8 16:53:30

If it’s on CS, you’ll see [cs] at the end of the line, otherwise nothing


phanthero
2020-11-8 16:54:15

i see, thanks. 7.4 is the version that they started releasing the CS beta it seems


phanthero
2020-11-8 18:06:05

Are mutable lists and pairs not passed by refence? #lang racket (define lis (mcons "joe" '())) (define (fun l) (set-mcar! l "jojo")) (displayln lis) this seems to just print {"joe"} instead of {"jojo"}


soegaard2
2020-11-8 18:08:07

There is no difference between immutable and mutable pairs when it comes to passing them along as arguments to functions.


soegaard2
2020-11-8 18:09:24

I think, you forgot to call fun : #lang racket (define lis (mcons "joe" '())) (define (fun l) (set-mcar! l "jojo")) (fun lis) (displayln lis)


phanthero
2020-11-8 18:42:14

oh ya, derp. I think the problem with the immutable pair is that you can’t mutate it, so once you have it inside a function, you must return the immutable pair and set it to whatever you want it to be. Eg. #lang racket (define lis1 (cons 1 2) (define (fun l) (map (add1 l)) (define lis2 (fun lis1)) whereas if you had mutable lists and just didn’t care about lis1 you don’t have to deal with two separate lists

Maybe immutability is better for functional programming though


michalbric
2020-11-8 19:45:33

@michalbric has joined the channel


notjack
2020-11-8 19:49:41

notjack
2020-11-8 19:54:59

Immutability is very nice for functional programming, yes


sorawee
2020-11-8 20:08:55

indeed, thanks @mflatt!!!


phanthero
2020-11-8 21:38:14

I was making a hash table with keys being symbol numbers, i.e. i read in input from stdin, and it was probably read in as the string "42", then I applied (string->symbol input) , and then using (hash-set! my-hash input (add1 input)) I set the values to 1 + the input. Now when I go fetch (hash-ref my-hash '42 #f), I get #f for some reason? When I print my-hash, the key is printed as \|42\|, but I thought that was equivalent to '42? I am very confused about symbols in Racket


soegaard2
2020-11-8 21:41:38

The syntax '<datum> means produce a valuate that prints as <datum> . Therefore ’42 produces the number 42. Symbols and strings are similar. If you turn the string “42” into a symbol, you get a symbol whose spelling is “42”. That’s different from the number 42.


notjack
2020-11-8 21:42:17

as a rule of thumb, try to avoid quoting things other than symbols


notjack
2020-11-8 21:42:38

e.g. don’t write '42 or '"foo"


soegaard2
2020-11-8 21:42:42

You could use (read-from-string input) instead of (string->symbol input) if you intended to use the number 42 as the key.


soegaard2
2020-11-8 21:44:00

Note: Only in the case <datum> is an identifier, '<datum>` will produce a symbol.


soegaard2
2020-11-8 21:46:02

Now let’s say you want to use quote-syntax to produce the same symbol as (string->symbol "42"). You can’t write '42 since that produces the number 42. Instead one can write '\|42\|. The idea is that | | delimits the name of the symbol. This is also used, when a symbol has a space in its spelling (which usually is a bad idea).


notjack
2020-11-8 21:47:00

I’m not sure you need symbols at all in this case. Why not make a hash with numbers as keys? You can turn strings into numbers using string->number.


phanthero
2020-11-8 21:53:57

OK, I’ve used symbols like '\( before, as it looks like a constant rather than a string I guess? I’m not sure but I could have used just the string. I didn’t know that '42 or '"foo produced numbers and strings respectively though. This whole idea of “spelling” is new to me..

BTW > Symbols and strings are similar. If you turn the string “42” into a symbol, you get a symbol whose spelling is “42”. That’s different from the number 42. Doing (string->symbol "42") gives you '\|42\| when printed instead, so I guess it’s not printed as 42 as you might expect.. So is “spelling” defined as the “look” of the symbol when it is printed or something else?


phanthero
2020-11-8 21:54:51

In my case, I don’t really need to do arithmetic so I thought it would be cool to use symbols instead of numbers instead, since I at least thought it doesn’t make a differnece


soegaard2
2020-11-8 21:56:32

“Same spelling” is my way of stating the first line in the docs on symbols: > A _symbol_ is like an immutable string, but symbols are normally https://docs.racket-lang.org/reference/reader.html?q=symbol#%28tech._interned%29\|interned, so that two symbols with the same character content are normally https://docs.racket-lang.org/reference/Equality.html?q=symbol#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29\|eq?.


notjack
2020-11-8 22:02:38

also note that to write a single character, the syntax is #\(


notjack
2020-11-8 22:03:09

for example, the string "apple" is made up of the characters #\a #\p #\p #\l #\e


phanthero
2020-11-8 22:03:17

I don’t really understand “interned” even after reading the documentation, but basically I only would need to worry about this property if I am working with the rich macros/DSL creation features of Racket right?


notjack
2020-11-8 22:03:36

You almost never need to worry about this property, even when making macros


notjack
2020-11-8 22:03:47

You only need to worry about this when using eq? instead of equal?


notjack
2020-11-8 22:03:59

and in general, things go best if you just use equal? everywhere by default


phanthero
2020-11-8 22:05:55

Yeep, I specifically wanted the symbol form of ( since it mended well with other symbols. I was trying to build a sort of language in this case, and all of these symbols are basically constants, so I thought it would be better to use symbols. Eg. 'int 'while '= '( etc. Then I figured out it doesn’t work with single characters by default, so I used the escape character \


soegaard2
2020-11-8 22:11:04

Regarding interning: A symbol is like an immutable string, but symbols are normally interned, so that two symbols with the same character content are normally eq?. When you use 'foo or (string->symbol "foo") several times you will always get the same value. Not equivalent values but the same value (in memory). This makes comparisons like (eq? x 'foo) fast - it is a simple comparison of adresses. These symbols are interned. You get the standard foo-symbol.

Now if for some reason (and it is rare indeed) you need a symbol 'foo not eq? to the the standard foo symbols, you can request a “non-interned” symbol. It behaves the same as a normal symbol, but its address aren’t stored in the “big table of standard symbols”.


phanthero
2020-11-8 22:11:44

Is it more proper to write '\( or '\|(\|?


notjack
2020-11-8 22:12:39

Either of those works and I don’t think anyone has a preference. For what it’s worth, the printed representation of that symbol will always be '\|(\| no matter which way you write it.


phanthero
2020-11-8 22:18:03

Another thing, I learnt this a while back (and using some of the teaching languages), but from what I remember they said that '('i 'like 'racket) , (cons 'i (cons 'like (cons 'racket (cons null) , and (list 'i 'like 'racket) are all the same thing, but is this different in #lang racket?


phanthero
2020-11-8 22:18:51

I think somewhere or the other this printed quote ... and I had no idea what that meant


notjack
2020-11-8 22:19:10

It’s still the same in #lang racket (though your first example has a bug, I think you meant '(i like racket))


notjack
2020-11-8 22:19:20

I prefer to write things in the (list 'i 'like 'racket) form


phanthero
2020-11-8 22:29:48

how are '(i like racket) and '('i 'like 'racket) different? the first one seems like it would have produced an error since none of i like racket are not defined anywhere, but .. this does seem to print this: Welcome to Racket v7.3. > '(i like racket) '(i like racket) > (list 'i 'like 'racket) '(i like racket) > '('i 'like 'racket) '('i 'like 'racket) but these things are doing something different. So in general, symbols never print with the quote, right? Should we just assume something is wrong if we ever print the symbol literally with a quote?


phanthero
2020-11-8 22:31:45

Would Section 1 of the Racket Reference be a good place to read that would explain these types of things?


soegaard2
2020-11-8 22:33:16

The reader turns 'foo into (quote foo) and then quote determines what to do.

Here are some nice answers on symbol / quote related questions: https://stackoverflow.com/questions/30150186/what-does-backtick-mean-in-lisp/30150276#30150276




phanthero
2020-11-8 22:44:41

Thanks!


rokitna
2020-11-8 23:50:13

'42 evaluates to a number, not a symbol. That’s why you can do arithmetic with it. You can check with number? and symbol?


rokitna
2020-11-8 23:52:16

the symbol whose string name is "42" can be written using \|42\|, as in (symbol? '\|42\|)


rokitna
2020-11-8 23:54:40

it’s just a non-shorthand syntax for symbols; for instance, 'foo is a shorthand for '\|foo\|


phanthero
2020-11-8 23:56:14

Thanks! I get the meaning of quote now. What do you mean by “string name” btw?


rokitna
2020-11-8 23:56:36

the shorthand is quirky in that it doesn’t let you write all the same symbols (in this case, it doesn’t encompass the ones whose names look like numbers)


rokitna
2020-11-8 23:57:10

the symbol 'foo has a name of "foo"


rokitna
2020-11-8 23:58:12

most symbols are in one-to-one correspondence with strings (with the exception of Racket’s uninterned symbols and unreadable symbols)


rokitna
2020-11-8 23:58:33

symbol->string gets the name of a symbol


phanthero
2020-11-9 00:02:37

Okay, so another way of putting it might be what’s in between the bars for the longhand format for symbols, eg: “foo” for '\|foo\| and “42” for '\|42\|


rokitna
2020-11-9 00:18:08

yeah


rokitna
2020-11-9 00:19:49

the || syntax can have escape sequences in it just like the "" syntax can


rokitna
2020-11-9 00:20:20

for instance, \|foo\\|bar\|


rokitna
2020-11-9 00:21:06

so the name isn’t necessarily exactly what’s between the bars, but it usually is