scheme

In R6RS Scheme, is there a way to get the current environment for use with eval?

我怕爱的太早我们不能终老 提交于 2019-12-04 05:35:01
Is there any way in R6RS Scheme to obtain the current environment and then pass it as the second argument to eval ? For example, what should the question marks be for the following expression to return 9? (let ((x 4) (y 5)) (eval '(+ x y) ???)) No, there is no such thing in R6RS. Some rare implementations might support something like that, but in the overwhelming majority (including eval in other languages!) this cannot be done. The reason for that is simple: it breaks compilation, since it leads to making two functions distinguishable based on local names, and in some cases can also prohibit

MIT Scheme Message Passing Abstraction

Deadly 提交于 2019-12-04 05:30:39
问题 In a Computer Science course I am taking, for homework, we were tasked with several different questions all pertaining to message passing. I have been able to solve all but one, which asks for the following: Write a mailman object factory (make-mailman) that takes in no parameters and returns a message-passing object that responds to the following messages: 'add-to-route : return a procedure that takes in an arbitrary number of mailbox objects and adds them to the mailman object's “route”

Get the middle elements from List in scheme

情到浓时终转凉″ 提交于 2019-12-04 05:27:40
问题 I'm new to scheme , can someone please give me ideas on how to get , "the middle element from a list?" 回答1: Here's my solution. It's based on a tortoise-and-hare algorithm (which is used in any kind of list traversal where you need to detect circular lists), so it doesn't do any more work than a sane list traversal has to do anyway. :-) (define (middle-elements lst) (if (null? lst) '() (let loop ((tortoise lst) (hare (cdr lst))) (cond ((eq? tortoise hare) #f) ((null? hare) (list (car tortoise

Execute command line from Scheme (Guile)

混江龙づ霸主 提交于 2019-12-04 05:24:59
The question is described on the title, basically I'd like to execute a command line from scheme, let's say 'ls' and obtaining the output. So my questions are: Is it possible? How? Thanks a lot in advance! By the way I use Guille. You need one of these system and system* . Example: (system "ls") From the documentation: Guile Reference — Scheme Procedure: system [cmd] — C Function: scm_system (cmd) Execute cmd using the operating system's “command processor”. Under Unix this is usually the default shell sh. The value returned is cmd's exit status as returned by waitpid, which can be interpreted

Scheme - find most deeply nested lists

我怕爱的太早我们不能终老 提交于 2019-12-04 05:23:54
问题 I need to find the leaves in a list in Scheme. For example, if I have (1 (2 3) (4 (5) (7 (8) (10 11 12)))))) , my leaves are (8) and (10 11 12) . So my function will return (1 (2 3) (4 (5) (7 leaf1 leaf2))))) . Definition: a leaf is an element with the deepest nesting possible. Examples: In (1 (2 (3))) the element (3) is a leaf. In ((1 2) (3 4)) the elements (1 2) and (3 4) are leaves. I tried to use the map function, that will check if the list is composed from lists. If it is - so I call

Good simple algorithm for generating necklaces in Scheme?

强颜欢笑 提交于 2019-12-04 05:20:14
A k-ary necklace of length n is an ordered list of length n whose items are drawn from an alphabet of length k, which is the lexicographically first list in a sort of all lists sharing an ordering under rotation. Example: (1 2 3) and (1 3 2) are the necklaces of length 3 from the alphabet {1 2 3}. More info: http://en.wikipedia.org/wiki/Necklace_(combinatorics) I'd like to generate these in Scheme (or a Lisp of your choice). I've found some papers... Savage - A New Algorithm for Generating Necklaces Sawada - Generating Bracelets in Constant Amortized Time Sawada - Generating Necklaces with

I'm trying to figure out how to incorporate 3 variables into my tail recursion code for racket

旧巷老猫 提交于 2019-12-04 05:19:39
问题 Write a tail recursive function called popadd that models a population with P people at time t = 0 and adds d people per year. (define (popadd t P) (if (= t 0) P (+(popadd( - t 1) P)d)) ) but, of course, I get the error that d hasn't been defined yet, which is true. I tried adding it as an input, but as a return I get the number inserted for D. 回答1: You can simply pass along another parameter to the recursion: (define (popadd t P d) (if (= t 0) P (+ d (popadd (- t 1) P d)))) Or you can define

Scheme let statement

六眼飞鱼酱① 提交于 2019-12-04 05:16:48
In scheme which is a functional programming language, there is no assignment statement. But in a let statement (let ((x 2)) (+ x 3)) You are assigning 2 to x , so why doesn't this violate the principle that there is no assignment statements in functional programming? The statement "Scheme which is a functional programming language" is incorrect. In Scheme, a functional-programming style is encouraged, but not forced. In fact, you can use set! (an assignment statement!) for modifying the value of any variable: (define x 10) (set! x (+ x 3)) x => 13 Regarding the let statement of the question,

getting the largest number in a list in scheme

倾然丶 夕夏残阳落幕 提交于 2019-12-04 05:06:36
问题 I do not understand why my function to get the largest number does not want to work. If I am thinking about this correctly, if the first atom is smaller than the second atom then you call the function minus the first one in the list, else you construct the first atom, largest one, with the rest of the list. relevant code: (define (getlargest a_list) (cond ((null? a_list) '()) ((< (car a_list) (cadr a_list)) (getlargest (cdr a_list))) (else (cons (car a_list) (getlargest(cdr a_list)))))) 回答1:

What is the Scheme function to find an element in a list?

回眸只為那壹抹淺笑 提交于 2019-12-04 04:46:19
I have a list of elements '(a b c) and I want to find if (true or false) x is in it, where x can be 'a or 'd, for instance. Is there a built in function for this? If you need to compare using one of the build in equivalence operators, you can use memq , memv , or member , depending on whether you want to look for equality using eq? , eqv? , or equal? , respectively. > (memq 'a '(a b c)) '(a b c) > (memq 'b '(a b c)) '(b c) > (memq 'x '(a b c)) #f As you can see, these functions return the sublist starting at the first matching element if they find an element. This is because if you are