racket

redirect browser in SimpleHTTPServer.py?

ε祈祈猫儿з 提交于 2019-12-24 02:05:02
问题 I am partially through implementing the functionality of SimpleHTTPServer.py in Scheme. I am having some good fun with HTTP request/response mechanism. While going through the above file, I came across this- " # redirect browser - doing basically what apache does" in the code". Why is this redirection necessary in such a scenario? 回答1: Imagine you serve a page http://mydomain.com/bla that contains <a href="more.html">Read more...</a> On click, the user's browser would retrieve http://mydomain

Racket - implementing the let* function using macro

﹥>﹥吖頭↗ 提交于 2019-12-24 01:54:54
问题 I need to implement my_let* using defmacro which works similarly to let*, but while let* is expanded to a series of nested let calls (behind the scenes), my_let* needs to be expanded to a single let call, and use the define statement to define the arguments i get. an example of using my_let*: (my_let* ((a 2) (b 3) (c (+ a b))) (+ a b c)) and the return value of this code should be 10. just as if it was use let*. the code above will be expanded in my_let* to the following: (let () (define a 2)

A simple Racket terminal interaction

你。 提交于 2019-12-23 19:40:29
问题 I'm just starting learning Racket-lang, and I want to write a simple program that reads from the terminal, does something with the input and responds. Here's that program in Python : while True : l = raw_input() print somefunction(l) How should I write the equivalent in Racket? 回答1: The equivalent of that program in racket would be this: (for ([line (in-lines)]) (displayln (some-function line))) That's if you want to just print the results to stdout. If you want to use the results as a value

Evaluating variables in Racket response/xexpr

为君一笑 提交于 2019-12-23 17:00:40
问题 I'm trying to make a simple bookmarking web-app in Racket. It's meant to receive a url as a CGI argument, and right now, I'm just trying to confirm I received it by reflecting it back. (define (start request) (response/xexpr (let* ([bindings (request-bindings request)] [url (if (exists-binding? 'url bindings) (extract-binding/single 'url bindings) "NO URL")]) `(html (head (title "TITLE")) (body (h2 "TITLE") (p "URL = " url)) )))) However, instead of seeing what I expect to see .. which is a

Where does a make- function come from?

泪湿孤枕 提交于 2019-12-23 15:51:38
问题 This code works: (define list-of-events (for/list ([(date code) (in-query odc "select date, code from attendance where student_id = ? and term_code = ?" "12345" "654321")]) (make-attendance-event date code))) However, when I try to duplicate the behavior for another table, the parallel item to make-attendance-event complains about it being an "unbound identifier". Now, where does make-attendance-event come from? 回答1: The identifier make-attendance-event came from a (define-struct attendance

Unable to use eval on user input in Racket

人走茶凉 提交于 2019-12-23 09:50:35
问题 I'm currently learning Scheme (using Racket), but one of the challenges I'm coming upon is trying to execute the following bit of code, which is meant to execute Racket code from user input using eval : (display (eval (read))) Here's some of the weird behavior I've observed so far: (display (eval (read))) in the definition window prompts for keyboard input, as expected, when the definitions are run. However, providing the input ((lambda (x) (+ x 1)) 1) gives the error ?: function application

How to check if a list contains only #t

余生长醉 提交于 2019-12-23 08:50:08
问题 I was trying with the following code in racket and MIT scheme, surprise me that the compiler throw err (foldr and #t '(#t #t #f)) Is there any way to use reduce/fold way to check if a list contains only true or false? I know a lambda can do the job, but it really make we wonder why this is not a valid code. I remember I can do it in Haskell..... TIA. 回答1: and is a macro, so it doesn't have a value by itself. Specifically, it short-circuits evaluation, and using it as you tried to will not

zip function in Racket/Scheme

隐身守侯 提交于 2019-12-23 07:47:14
问题 Given two lists, return a list whose elements are lists of size two, such that for the i -th list, the first element is the i -th element of the first original list, and the second element is the i -th element of the second original list. If one list is smaller than the other, the resulting list is of the smallest size; and so if one of the lists is empty, return an empty list. For example: > (zip '(1 2) '(3 4)) '((1 3) (2 4)) > (zip '(1 2 3) '()) '() > (zip '() '(4 5 6)) '() > (zip '(8 9) '

Solving a puzzle in Racket

喜夏-厌秋 提交于 2019-12-23 03:22:40
问题 I tried to solve this puzzle https://puzzling.stackexchange.com/questions/40094/who-committed-the-crime using Racket. A crime has been carried out by one person, there are 5 suspects. Each suspect is asked under polygraph who they think committed the crime. Their answers are as follows: Terry : It wasn't Carl, It was Steve Steve : It wasn't Matt, It wasn't Carl Matt : It was Carl, It wasn't Terry Ben : It was Matt, It was Steve Carl : It was Ben, It wasn't Terry The polygraph showed that each

How to print a string in backward, in scheme?

被刻印的时光 ゝ 提交于 2019-12-22 23:11:23
问题 I know if I write my scheme code in the following way and type in (word ‘(a b c)), it will out put the list in the same order. Could you please tell me if there was a way I can print it out in opposite order. Ex- (list ‘c ‘b ‘a). it needs to be the user's input I print out in opposite order. So, I can't call it (reverse '(a b c)). since the user input can be something like '(x y z). Thanks a lot. (define(word x ) (if(null? x) x (cons(car x)(word (cdr x))))) (word '(a b c)) (list 'a 'b 'c) 回答1