scheme

Differences between two similar definitions

℡╲_俬逩灬. 提交于 2019-12-11 06:56:15
问题 Is there any difference between (define make-point cons) and (define (make-point x y) (cons x y)) ? Is one more efficient than the other, or are they totally equivalent? 回答1: There are a few different issues here. As Oscar Lopez points out, one is an indirection, and one is a wrapper. Christophe De Troyer did some timing and noted that without optimization, the indirection can take twice as much time as the indirection. That's because the alias makes the value of the two variables be the same

When to use define and when to use let in racket

寵の児 提交于 2019-12-11 06:47:00
问题 I'm learning racket and I have a question about when to use define and when to use let . I have this function: (define my-function (lambda (param1 param2 list1) (/ (count (lambda (x) (define a (param1 (remove x list1))) (define b (drop-right x 1)) (define c (param2 a x-sin-clase)) (eq? (last x) (last c))) (cdr list1)) (length (cdr list1))))) Without knowing what the above function does. Is it correct to use define inside the function body? I have read somewhere that define is used to declare

Extending the Charme Interpreter

社会主义新天地 提交于 2019-12-11 06:44:32
问题 I need to extend the Charme interpreter (described here) by adding a primitive procedure <= to the global environment. I know that to do this I also need to define a procedure that implements the primitive, and modify initializeGlobalEnvironment to install the primitive. This is what I have for initializeGlobalEnvironment -- def initializeGlobalEnvironment(): global globalEnvironment globalEnvironment = Environment(None) globalEnvironment.addVariable('true', True) globalEnvironment

Make a destructive reverse! function in scheme

点点圈 提交于 2019-12-11 06:39:19
问题 I need to create a program that will reverse a list destructively. For example lets say.. scm> (define L (list 1 2 3 4)) scm> (reverse! L) (4 3 2 1) scm> L (1) Where L becomes the last element of the reversed list. I know I am supposed to use set-cdr! somehow but cannot figure out how to implement it. 回答1: Because this looks like homework, I can't give you a straight answer. I'll show you the general structure of the solution, so you can figure out the details and fill-in the blanks: (define

Meaning of 'quote in Lisp

吃可爱长大的小学妹 提交于 2019-12-11 06:26:20
问题 This question arose when reading SICP . Why (list 'quote '(a b c)) evaluated by the interpreter ( R5RS in Dr.Racket ) as '(a b c) . For me it should be (quote (a b c)) . For instance (list 'quot '(a b c)) is evaluated as (quot (a b c)) . What is so special in the 'quote ? 回答1: It also took me a while to understand this problem. But it's just your good-hearted lisp interpreter showing (quote (a b c)) in it's equivalent form '(a b c). Since there is no such equivalence/syntactic sugar for

How to load a huge file into a String or list in Racket?

本秂侑毒 提交于 2019-12-11 06:18:41
问题 I have a HUGE file that I need to do operations on. Huge as in approx. half a million words. I just want to read it into a list or String so I can do things with it later. Also I know I could load it into a string using file->string OR use file->list, file->lines, but these seem to take waayy too long. Is this the right way to load it into a list?: (define my-list (with-input-from-file "myFile.txt" read)) Whenever I run my program I just get the first line printed out. Seems to work for

Regular expression for contents of parenthesis in Racket

纵饮孤独 提交于 2019-12-11 06:17:14
问题 How can I get contents of parenthesis in Racket? Contents may have more parenthesis. I tried: (regexp-match #rx"((.*))" "(check)") But the output has "(check)" three times rather than one: '("(check)" "(check)" "(check)") And I want only "check" and not "(check)". Edit: for nested parenthesis, the inner block should be returned. Hence (a (1 2) c) should return "a (1 2) c". 回答1: Parentheses are capturing and not matching.. so #rx"((.*))" makes two captures of everything. Thus: (regexp-match

How do you find where an error has occurred in MIT scheme?

。_饼干妹妹 提交于 2019-12-11 06:16:21
问题 When you get an error in MIT scheme it doesn't tell you where the error occurred. For example, it just prints something like this: ;Unbound variable: top-left ;To continue, call RESTART with an option number: ; (RESTART 3) => Specify a value to use instead of top-left. ; (RESTART 2) => Define top-left to a given value. ; (RESTART 1) => Return to read-eval-print level 1. How do I find where this error occurred in my code? 回答1: In mit-scheme, if you are using the REPL from a shell, you can call

Why does the nested quasiquote not unwrap splice-unquote?

江枫思渺然 提交于 2019-12-11 06:03:46
问题 Why does the first macro unquote-splicing not produce (quasiquote (unquote (quasiquote 1 2))) , but the second example replaces the value? (define x (list 1 2)) (t '(quasiquote (unquote (quasiquote (unquote-splicing x)))) (quasiquote (quasiquote (unquote (quasiquote (unquote-splicing x)))))) (t '(quasiquote (unquote (quasiquote (unquote (1 2))))) (quasiquote (quasiquote (unquote (quasiquote (unquote (unquote x))))))) 回答1: In the first example (quasiquote (quasiquote (unquote (quasiquote

Difference between '(()) and (cons null null)

醉酒当歌 提交于 2019-12-11 05:59:22
问题 I am confused about the difference between '(()) and (cons null null) in scheme. The code below show that b and c are completely the same thing. (define (dup2 x) (let ((d '(()))) (set-car! d (car x)) (set-cdr! d (cdr x)) d)) (define a '(1 2)) (define b (dup2 a)) (define c (dup2 a)) (set-car! b 2) > c ;; --> (2 2) However, when I used dup instead of dup2 : (define (dup x) (let ((d (cons null null))) (set-car! d (car x)) (set-cdr! d (cdr x)) d)) (define a '(1 2)) (define b (dup a)) (define c