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!)
Does length
take constant time for list
? Probably for vector, but not sure about implementation of list in particular
It takes linear time (sadly)
list?
Takes amortised constant time though.
Ok I see. Should be easy enough to define
a struct
which wraps around a list to track length though
Is there an easy way to check this kind of runtime efficiency (is it in the docs somewhere?)
Time (and memory) complexity is one thing I wish Racket doc could be clearer about.
Hmmm, the implementation of Racket is public somewhere or the other right? Is there an easy way to lookup the corresponding code?
Since Racket is written in Racket things should be easy to understand for everyone too
Not for low level stuff like lists. Those are defined in Chez Scheme layer.
(and C code for the BC variant)
Here’s the very same question last month. You can follow the links for the source code: https://racket.slack.com/archives/C09L257PY/p1601916642152000
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
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?
The plain racket version: http://pasterack.org/pastes/87386 The typed racket version (that raises an error): http://pasterack.org/pastes/44174
In DrRacket, is the first line of the interaction window. On the command line, it’sracket -v
@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
If it’s on CS, you’ll see [cs]
at the end of the line, otherwise nothing
i see, thanks. 7.4 is the version that they started releasing the CS beta it seems
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"}
There is no difference between immutable and mutable pairs when it comes to passing them along as arguments to functions.
I think, you forgot to call fun
: #lang racket
(define lis (mcons "joe" '()))
(define (fun l)
(set-mcar! l "jojo"))
(fun lis)
(displayln lis)
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 has joined the channel
Your wish was granted https://github.com/racket/racket/commit/d2b954c428f73e3f850d9a5e259ac517e7b44ced (thanks @mflatt!!)
Immutability is very nice for functional programming, yes
indeed, thanks @mflatt!!!
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
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.
as a rule of thumb, try to avoid quoting things other than symbols
e.g. don’t write '42
or '"foo"
You could use (read-from-string input)
instead of (string->symbol input)
if you intended to use the number 42 as the key.
Note: Only in the case <datum>
is an identifier, '<datum>
` will produce a symbol.
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).
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
.
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?
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
“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?.
also note that to write a single character, the syntax is #\(
for example, the string "apple"
is made up of the characters #\a #\p #\p #\l #\e
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?
You almost never need to worry about this property, even when making macros
You only need to worry about this when using eq?
instead of equal?
and in general, things go best if you just use equal?
everywhere by default
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 \
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”.
Is it more proper to write '\(
or '\|(\|
?
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.
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?
I think somewhere or the other this printed quote ...
and I had no idea what that meant
It’s still the same in #lang racket
(though your first example has a bug, I think you meant '(i like racket)
)
I prefer to write things in the (list 'i 'like 'racket)
form
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?
Would Section 1 of the Racket Reference be a good place to read that would explain these types of things?
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
Thanks!
'42
evaluates to a number, not a symbol. That’s why you can do arithmetic with it. You can check with number?
and symbol?
the symbol whose string name is "42"
can be written using \|42\|
, as in (symbol? '\|42\|)
it’s just a non-shorthand syntax for symbols; for instance, 'foo
is a shorthand for '\|foo\|
Thanks! I get the meaning of quote
now. What do you mean by “string name” btw?
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)
the symbol 'foo
has a name of "foo"
most symbols are in one-to-one correspondence with strings (with the exception of Racket’s uninterned symbols and unreadable symbols)
symbol->string
gets the name of a symbol
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\|
yeah
the || syntax can have escape sequences in it just like the "" syntax can
for instance, \|foo\\|bar\|
so the name isn’t necessarily exactly what’s between the bars, but it usually is