scheme

Lambda and the Environment Model

被刻印的时光 ゝ 提交于 2020-01-22 21:31:27
问题 I need help drawing the relevant portions of the environment model diagram when evaluating this code: Scheme>(define x 10) Scheme> ((lambda (x y) (+ (y 3) x)) 6 (lambda (w) (* x 9))) I need to make sure and write each lambda body next to the environment in which it is being evaluated. Okay I know that there is only one define so most of the work will be done by “anonymous” or “nameless” functions and these will still show up in various ways in the environment model diagram 回答1: In addition to

Lambda and the Environment Model

為{幸葍}努か 提交于 2020-01-22 21:31:08
问题 I need help drawing the relevant portions of the environment model diagram when evaluating this code: Scheme>(define x 10) Scheme> ((lambda (x y) (+ (y 3) x)) 6 (lambda (w) (* x 9))) I need to make sure and write each lambda body next to the environment in which it is being evaluated. Okay I know that there is only one define so most of the work will be done by “anonymous” or “nameless” functions and these will still show up in various ways in the environment model diagram 回答1: In addition to

How to convert string to variable-name in scheme

落花浮王杯 提交于 2020-01-21 05:26:10
问题 How can I achieve below in Scheme REPL? Create a variable name from a string. =>(define (string->variable-name "foo") 12) =>foo 12 =>(+ foo 8) 20 In Common Lisp, this should be => (set (intern "ANY-TEXT") 5) => ANY-TEXT 5 How do I build a #procedure like "string->variable-name" (and "variable-name->string") ? Thanks a lot. 回答1: If what you're passing to string->variable-name is always a string literal (i.e., not a variable that contains a string), you can do that using a syntax-case macro

The Little Schemer evens-only*&co

筅森魡賤 提交于 2020-01-20 19:14:10
问题 I'm having difficulty understanding what's going on with The Little Schemer's evens-only*&co example on page 145. Here's the code: (define evens-only*&co (lambda (l col) (cond ((null? l) (col '() 1 0)) ((atom? (car l)) (cond ((even? (car l)) (evens-only*&co (cdr l) (lambda (newl product sum) (col (cons (car l) newl) (opx (car l) product) sum)))) (else (evens-only*&co (cdr l) (lambda (newl product sum) (col newl product (op+ (car l) sum))))))) (else (evens-only*&co (car l) (lambda (newl

deep-reverse for scheme

╄→尐↘猪︶ㄣ 提交于 2020-01-17 08:44:50
问题 When entering the list of ((1 2) (3 4)), I want to reverse it, but not so it's ((3 4) (1 2)), which is what reverse does, so I'm trying to write a deep-reverse procedure: (define (deep-reverse l) (cond ((null? l) nil) (not (pair? (car l)) l) (else (append (deep-reverse (cdr l)) (list (car l)))))) but it just throws back ((1 2) (3 4)). What's wrong and how do I get this to work? 回答1: Try: (define (deep-reverse l) (map reverse l)) The above is the simplest possible answer; a real answer depends

How to get the even elements in a list recursively

南笙酒味 提交于 2020-01-17 02:52:06
问题 I'm trying to create a function that will return the even numbered elements in a list. For example: (evens '(a b c d)) should return (b d) The code below seems to work for lists that have and odd numbers of elements, but if I give it a list with an even number of elements, it is incorrect. For example: (evens '(a b c d e)) will return (b d) But: (evens '(a b c d)) will return (a c) Any thoughts? Changed my code to: (DEFINE (evens lis) (cond ((null? lis) '()) (else (cons (cadr lis) (evens (cdr

How to implement asynchronous code that looks synchronous mimicking async / await?

僤鯓⒐⒋嵵緔 提交于 2020-01-16 05:28:10
问题 Otherwise said, I want to rely on epoll (or similar) to write asynchronous network code that looks like regular code that is without relying on callbacks. The code must look like synchronous code but unlike synchronous code instead of blocking to wait for network io, it must suspend the current coroutine and restart it when the file descriptor is ready. 回答1: My initial thought to achieve that was relying on generators and yield . But this was a mistake that was partly mis-guided by the fact

How do I delete from a binary search tree in Lisp

南笙酒味 提交于 2020-01-15 18:32:28
问题 How can I delete a node from a BST? I need an algorithm to do that in Dr. Scheme. 回答1: You basically toss the BST you have now, and create a new one sans the element. You can do this by recursively descending the tree. If your item is less than the root datum, create a BST whose root and greater-than branch is copied from what you have now, but whose less-than branch is the result from a recursive call. It's very similar to how you add a node, but when you get to the one you were searching

How to append to a file using Scheme?

穿精又带淫゛_ 提交于 2020-01-15 09:44:33
问题 I am using TinyScheme (actually Script-Fu in GIMP), and cannot find a good way to open a file and append a line of text to it. I am trying to log some info to the file for debugging, and transcript-on doesn't seem to be implemented... Right now I am scraping along by reading the entire file (one character at a time!), concatenating that into a string, concatenating my text to the end of that, then writing it all out to the file again. There must be a better way! 回答1: It's going to be

Can't hide “Preferences” item in edit-menu

坚强是说给别人听的谎言 提交于 2020-01-15 09:32:11
问题 How do I hide "Preferences" item in this picture when "undo and redo" items are able to hide? I tried using (preferences:hide-dialog) but there was no difference in GUI. #lang racket/gui (require framework) (define menu-super-frame% (frame:standard-menus-mixin frame:basic%)) (define menu-frame% (class menu-super-frame% (inherit get-file-menu set-icon) ;hiding items in edit menu (define/override (edit-menu:create-undo?) #f) (define/override (edit-menu:create-redo?) #f) (super-new))) (define