@oldsin I haven’t used brag
so I can’t say how error reporting can be improved there. An alternative is to use the lexer generator <https://docs.racket-lang.org/parser-tools/Lexers.html?q=lexer>
. Here you can choose whether to parse the tokens using your own recursive descent functions, or to use the parser generator (LALR(1)) <https://docs.racket-lang.org/parser-tools/LALR_1__Parsers.html?q=lexer>
It has the same quirks as other yacc/bison style parsers.
Yet another alternative is: parsack
https://docs.racket-lang.org/parsack/index.html?q=parsec
@earl has joined the channel
Dear all, i’m trying to test a thing that get a syntax-object as an input and outputs a datum as an output. Is there a way to write down the syntax object in a file ? Here is what drracket shows me :
Click the little blue arrow at the left.
This folds out the syntax object.
I expected to be able to build an object like that so that they are equal like (syntax ...)
but it’s not so simple apparently
You can now click at individual identifiers to see the associated source location etc.
ok thx!
Also: it’s difficult to use equal?
to compare syntax objects.
Is there any convenient way to cast a struct into it’s child (providing the extra values the child struct type needs)
I checked to see if this works, but it doesn’t: (struct foo (x y))
(struct kid foo (z))
(define f (foo 1 2))
(struct-copy kid f (z 3))
Is this an ok idea? (require racket/struct)
(apply kid (reverse (cons 3 (reverse (struct->list f)))))
Although the structs have to be transparent, but that’s ok
Okay I think I got it, but I followed the spirit of the chapter and the way these books usually go: “first we write clunky bad code, then we find better ways.”
#lang racket
(define (square x)
(* x x))
(define (sum-of-squares x y z)
(cond ((> x y z) (+ (square x) (square y)))
((< x y z) (+ (square y) (square z)))
((> y x z) (+ (square y) (square x)))
((< x z y) (+ (square z) (square y)))
((> x z y) (+ (square x) (square z)))
((> z x y) (+ (square z) (square x)))))
(sum-of-squares 4 5 6)
(sum-of-squares 4 6 5)
(sum-of-squares 6 5 4)
(sum-of-squares 5 6 4)
(sum-of-squares 5 4 6)
(sum-of-squares 6 4 5)
> 61
> 61
> 61
> 61
> 61
> 61
Little question: is this or IRC a better place to go?
are you telling me to go to IRC instead?
@earl both are good i guess. :slightly_smiling_face:
Your preference might depend on how old you are. :wink:
… or how old you feel!
IRC makes me feel oooollld
@abbyrjones72 just a little tip. You can wrap your code with ```
It will make your code looks like this
(define (hello)
world)
oh
It’s backtick
ty lol
The key between esc
and tab
Yup :slightly_smiling_face:
Coffee hasn’t kicked in
@abbyrjones72 also, try (sum-of-squares 2 1 2)
. What’s the expected output?
okay se
(define (sum-of-squares x y z)
(cond ((>= x y z) (+ (square x) (square y)))
((<= x y z) (+ (square y) (square z)))
((>= y x z) (+ (square y) (square x)))
((<= x z y) (+ (square z) (square y)))
((>= x z y) (+ (square x) (square z)))
((>= z x y) (+ (square z) (square x)))))
oops
ty @sorawee
np!
i tested (sum-of-squares 2 2 2) also and it dropped one like it was supposed to. :heart:
is there an "applicative order vs. normal order’ for dummies anywhere? going to make that something I learn today
@abbyrjones72 “applicative order” (more often known as “call by value”) means that we evaluate all the arguments to a function before we do the function call
“normal order” (more often known as “call by name”) means that we do the function call first, and only evaluate the arguments if we need them
so in normal order, it is possible to divide by zero accidentally?
in both you can do that
but it might happen at different places
and with normal order, you might not see the error sometimes when you would see it with applicative order
for example ((lambda (x) 5) (/ x 0))
produces an error in applicative order, but 5 in normal order
oh I see!
you can see this by trying them both in regular #lang racket
and in #lang lazy
normal order looks for the first true evaluation of arguments?
it waits until it really needs the result of the argument to evaluate it
ok
it will be something that takes me a while to fully grasp, but it feels like it’s worth it to learn
Normal order sends (/ x 0)
as the parameter, without evaluating it first
ohh
that seems like it could be good and bad at the same time
yes, indeed
that you both so much. If I ask too many questions, please let me know. I hit that “I never wanted to be a website developer/application developer” wall a while ago and have been investigating computer science as a sort of autodidactic pursuit.
Want to make sure I understand compound expressions here: my understanding is in comments.
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b)) ; if b > 0, then we assign + to b, else we assign - to b, and then add to a
“assign”?
bad wording
yes, (if (> b 0) + -)
evaluates to +
if b is greater than 0 and -
otherwise
ty that’s what I meant
Maybe you meant (+ a (<something> b))
where that something is either an identity function or a negation function?
you can use the substitution method, so ((if (> b 0) + -) a b)
evaluates to (+ a b)
if b > 0 and (- a b)
if not
no, this is a problem from SICP and the formula was provided to me. I just had to explain it
i guess it was an introduction to compound expressions?
note that b is only involved in the last operation, you don’t change it after the if
correct, I did get that :slightly_smiling_face:
https://racket.slack.com/archives/C06V96CKX/p1550490282236400 @hoshom I’d probably just pattern match? Something like (match f [(foo x y) (kid x y z)])
?
@andreiformiga Apparently RWops structs have a close method which can be called with io->close(io);
but I don’t have a clue how you could call that from racket, any ideas? Sorry to keep bothering you.
@mark.warren no problem but you don’t need to call close
I defined sdl-load-bmp
just like the SDL_LoadBMP
macro
if the second argument to SDL_LoadBMP_RW
is non-zero, the stream will be closed after being read
otherwise it would be weird because SDL_LoadBMP
does not return the RWops
@greg yes that works, too, thanks
@lspector has joined the channel
I see a lot in docs on web programming, but not how to deploy. If, for example, I follow the directions to make an Instant Servlet at https://docs.racket-lang.org/web-server/run.html#%28part._insta%29 then it will run in a browser on my own machine, but what do I do if I have a public html directory on a server and I want to put it there, so that anyone can run it by pointing their browser at a URL on the server?
I have a simple jargon question. Let’s say that I created the following definitions (define x '(1 2 3))
and (define y #(4 5 6))
and I want to refer to x
and y
collectively. What would I call them? In R, I would call them objects but that doesn’t strike me as the correct jargon for Racket.
“objects” is fine, unless the context of the discussion implies you’re talking about OO stuff
values?
~'(1 2 3)
is a value, but x
is not~
As an old programmer used to things like C, I’d probably call x
and y
“variables”. Even though, in Racket, we don’t usually mutate or assign new values (using set!
). (In other words, they don’t really “vary” :smile:)
More strictly, I might say that x
and y
are bindings whose values are that list and that vector, respectively ¯_(ツ)_/¯
though, looking closer… I wasn’t reading that quite right
I think there’s some text in the docs about the evaluation model, that talks about “locations” that normally have binding names like x
and y
, and in each location there are values.
@travis.hinkelman to some degree it might matter whether you’re referring to x
and y
themselves or to what they name. My earlier comment that “‘objects’ is fine” is (I think) true if you mean the latter.
use-mention distinction stuff
@greg In pure mathematics, they’re also called variables even though they don’t “vary”.
I would probably run the program on the server on a specific port (say 8080
) and then have nginx proxy requests to it
You would also want to do something to make sure your program keeps running on crashes, with upstart, supervisord or similar
Well I call them constables.
@benjamin.clos has joined the channel
Is there an idiomatic way to use a pipeline line operator \|>
like in Elixir? Very new to Racket and I saw this: https://docs.racket-lang.org/heresy/pipes.html
but it seems to be a completely different language?
Thanks @d_run! The serveerracket thing is actually the opposite of what I want, which is not to make a more complicated website but rather to put the simplest possible website, actually any Racket code at all, on the web. Your “nginx proxy requests” comment may be what I want, but I have no idea what that means :disappointed:. Also, no idea what “supervisord” means. Would love some guide that assumes only knowledge of Racket and shows how to put some Racket thing (maybe something simple like in the Instant Servelet example) on the web, for example on an academic server in which I can already put html
files, etc. It would be ideal if I could just put stuff there and it would run when accessed, but if I’d instead have to run a command and/or set something up to run the command after a reboot then that would also be fine if there were instructions for doing that that require no knowledge beyond knowing Racket. Does such a thing exist, or could it?
@benjamin.clos there are several packages that supply threading macros. You might be interested in this conversation from a few years back: https://groups.google.com/forum/#!msg/racket-users/uZnfMrbAE98/J-jbjxwyAgAJ
So, the word “server” is used in a lot of different ways, and there are various types of servers out there. It sounds like you’re talking about a very traditional shared hosting environment. Somewhere there is a computer (called a “server”) running a program like Apache (also called a “server”). Apache looks in some directory on that computer for your files and sends them out when someone requests the corresponding URL. (Apache can do more than this: I’m simplifying.) All of those things are “static” files, meaning that they are generated ahead of time and they don’t change in response to interactions with visitors. You can certainly use Racket to make static files (e.g. with Frog: https://docs.racket-lang.org/frog/), but that isn’t what the Racket Web Server is for. The web-server
library essentially gives you the Apache functionality (roughly) as a library that you can use from programs. It can serve static files, but it is mostly oriented to dynamic, interactive content: for example, processing input from a form and displaying some computed output. In this sense it is similar to server frameworks like node.js. Most traditional shared hosting environments like you seem to be describing aren’t really set up to run servers like Racket (or node.js, etc.). It is possible, but you would need a willing system administrator with root access. You could set up your Racket application to listen on some local port, and then use Apache as a reverse proxy to your Racket server. The way of running a Racket server on the public internet that requires the least sysadmin knowledge is to do it on a machine (perhaps a virtual machine, like an AWS instance) that isn’t already serving other web content with some other web server like Apache. In that case, you just start your Racket process as you do on your local machine, but tell it to run on port 80 (or 443) instead of 8000 or whatever. All of this requires at least some knowledge beyond Racket (and not specific to Racket), but I’m happy to help if I can.
@andreiformiga Ah cool. that call to close on the struct looked dodgy anyway.