racket

removing last element of a list(scheme)

元气小坏坏 提交于 2019-12-03 16:37:08
问题 So I have to remove the last element of a list in scheme. For example, let's say I have a list (1 2 3 4) . I need to return: (1 2 3) My idea: reverse(list) car(list) reverse(list) Is there a reverse function in scheme(racket)? 回答1: You wrote: "reverse, car, reverse". I believe you meant to write "reverse, cdr, reverse". There's nothing wrong with this solution; it's linear in the size of the list, just like any solution to this that uses the standard lists. As code: ;; all-but-last: return

Scheme add element to the end of list [closed]

独自空忆成欢 提交于 2019-12-03 16:01:41
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center . Closed 7 years ago . How can you add an element to the end of a list (before the empty) when only cons, first, rest, empty? and cond recursions can be used Chris Jester-Young Think about how you would implement append (or, more generally, think about how you would implement a

Racket URL dispatch

喜欢而已 提交于 2019-12-03 13:19:41
问题 I'm trying to hook up URL dispatch with Racket (formerly PLT Scheme). I've taken a look at the tutorial and the server documentation. I can't figure out how to route requests to the same servlets. Specific example: #lang scheme (require web-server/servlet) (require web-server/dispatch) (provide/contract (start (request? . -> . response/c))) (define (start request) (blog-dispatch request)) (define-values (blog-dispatch blog-url) (dispatch-rules (("") list-posts) (("posts" (string-arg)) review

Setting language with #lang in the REPL

天涯浪子 提交于 2019-12-03 11:24:00
问题 I would like to set the language in the REPL on the fly, with #lang , not using "-I" command-line argument. But this gives me the error "read: #lang not enabled in the current context". Is there a command-line switch that I'm missing? Or maybe a ",metacommand" I can use? The reason I need this is because I would like to be able to send an Emacs buffer to the Racket REPL, but that won't work if the file starts with #lang . 回答1: [Edit] I can't get C-x C-b to work with #lang either. But a buffer

What is scheme's equivalent of tuple unpacking?

假装没事ソ 提交于 2019-12-03 11:21:45
问题 In Python, I can do something like this: t = (1, 2) a, b = t ...and a will be 1 and b will be 2. Suppose I have a list '(1 2) in Scheme. Is there any way to do something similar with let ? If it makes a difference, I'm using Racket. 回答1: In racket you can use match, (define t (list 1 2)) (match [(list a b) (+ a b)]) and related things like match-define: (match-define (list a b) (list 1 2)) and match-let (match-let ([(list a b) t]) (+ a b)) That works for lists, vectors, structs, etc etc. For

Is there a way to view a function's source code from within the Racket REPL?

匿名 (未验证) 提交于 2019-12-03 09:13:36
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm currently trying to dive into Racket/Scheme a bit. I have an instance of the (X)REPL running next to my editor, which helps me immensely to explore the language. However, I can't seem to find an XREPL command or macro (or whatever) which would show me the source code of a function. All the needed parts seem to be there: XREPL's describe command knows the file: -> ,describe string-join ; `string-join' is a bound identifier, ; defined in racket/string.rkt ; required directly and get-collects-search-dirs knows the path: -> (require setup

Could someone explain call/cc in simple words?

耗尽温柔 提交于 2019-12-03 06:57:20
I am studying the language racket and trying to grasp what call/cc is actually for. Could someone please explain it in simple words and give an example or two? Thanks. If you have an expression (+ (* 2 3) (/ 10 2)) a Scheme system will not evaluate everything at the same time but in parts. The order is not specified in Scheme but lets imagine it's from left to right (I think Racket always do left to right): You do (* 2 3) , the continuation to that would be to compute (/ 10 2) , then (+ result1 result2) . The way a Scheme system can do this is by transforming your code to Continuation passing

How do you load a file into racket via command line?

巧了我就是萌 提交于 2019-12-03 05:42:38
I have been trying to launch a racket program from the commandline (via 'racket') but have not been having success. According to the documentation (here http://docs.racket-lang.org/reference/running-sa.html#%28part._mz-cmdline%29 ) passing -f followed by a file should evaluate that file. However, I can't seem to get this to work. As a test, I made the following file: ;test.rkt #lang racket (define a 1) Then, running it in racket (supposedly loading the file) and attempting to recall the value of a: racket -f test.rkt -i Welcome to Racket v5.1.1. > a reference to undefined identifier: a My end

Is there an equivalent to Lisp's “runtime” primitive in Scheme?

拈花ヽ惹草 提交于 2019-12-03 04:36:40
According to SICP section 1.2.6 , exercise 1.22: Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds). I'm using DrScheme , where runtime doesn't seem to be available, so I'm looking for a good substitute. I found in the PLT-Scheme Reference that there is a current-milliseconds primitive. Does anyone know if there's a timer in Scheme with better resolution? current-milliseconds is a function that returns the current millisecond count from the system, but it

Why doesn't Scheme support first class environments?

倖福魔咒の 提交于 2019-12-03 04:22:47
问题 I've been reading through SICP (Structure and Interpration of Computer Programs) and was really excited to discover this wonderful special form: "make-environment", which they demonstrate to use in combination with eval as a way of writing modular code (excerpt from section 4.3 on "packages"): (define scientific-library (make-environment ... (define (square-root x) ...))) They then demonstrate how it works with ((eval 'square-root scientific-library) 4) In their example, they then go on to