scheme

Scheme accumulative recursion with lists

陌路散爱 提交于 2020-01-05 05:57:05
问题 How can I pass a list as a parameter to a function adding elements to it recursively,and have it unmodified when it comes out of recursion? I want to use the list at each level of recursion with the list having the values added by deeper recursion levels. To be more specific I want to do a DFS search on a graph and I want to store in the list the nodes I visited. 回答1: One method of doing this is just to return the list so you have access to it at higher levels of recursion. Another method is

Using Racket to Plot Points

送分小仙女□ 提交于 2020-01-05 05:50:11
问题 After spending some time looking at the documentation, searching the web, and experimenting at the prompt, I haven't succeeded at plotting points using Racket. Could someone post an example of how I'd go about plotting (0 1 2 3 4 5) for my x coordinates and (0 1 4 9 16 25) for the y coordinates. I think 1 good example will clear the problem up. 回答1: Based on the first example of the doc, and given that the function you want to plot already exists in Racket, it's as simple as: (require plot)

How can I unsplice a list of expression into code?

你。 提交于 2020-01-05 05:41:05
问题 I have an experiment for my project, basically, I need to embedded some s-expression into the code and make it run, like this, (define (test lst) (define num 1) (define l (list)) `@lst) ; oh, this is not the right way to go. (define lst `( (define num2 (add1 num)) (displayln num2))) I want the test function be like after test(lst) in racket code: (define (test lst) (define num 1) (define l (list)) (define num2 (add1 num) (displayln num2)) How can I do this in racket? Update The reason I would

Recursive Scheme Function Value Error

女生的网名这么多〃 提交于 2020-01-05 05:00:12
问题 I am writing a small hangman game in scheme and am getting a very weird issue that almost seems like a language specific one. In my game I have a variable that holds the number of mistakes allowed and on each recursive call of my game loop, I "let" the value to a new one if it needs to be changed. Here is some code to help visualize how I run the game loop. guessed_list - a list of string characters containing old guesses and one new guess (ex. '("a" "x" "b") where "a" is the new guess) game

How to use continuations in scheme?

淺唱寂寞╮ 提交于 2020-01-05 04:56:10
问题 I'm trying to understand call/cc operator in Scheme. I'm planing of implementing this in my JavaScript lisp. This is my simple code: (letrec ((x 0) (f (lambda (r) (set! x r) (display (r 20)) (display "10")))) (display (call/cc f)) (x "30")) I tough that it should print 20 then 30 then 10. But it create infinite loop (it keep printing 30). How this code should look like to display 3 values, call display 3 times? Should it be possible to create loops that don't consume stack with continuations?

Macro of [S:N] for in-range in Racket

回眸只為那壹抹淺笑 提交于 2020-01-05 03:48:15
问题 How can I create a macro so that S:N or [S:N] returns a range of numbers starting with S and ending with N (step 1). Basically, it should be able to use it in place of 'in-range'. I tried to create something similar to Curly brackets {} to replace 'begin' in Racket but could not. Edit: I tried following as suggested by @soegaard : my-top.rkt: #lang racket (define-syntax-rule (my-top S:N) (range S N) ) (provide (rename-out [my-top #%top])) test.rkt: #lang racket (require "my-top.rkt") (1:42)

Scheme prime numbers

萝らか妹 提交于 2020-01-05 03:03:33
问题 this is possibly much of an elementary question, but I'm having trouble with a procedure I have to write in Scheme. The procedure should return all the prime numbers less or equal to N (N is from input). (define (isPrimeHelper x k) (if (= x k) #t (if (= (remainder x k) 0) #f (isPrimeHelper x (+ k 1))))) (define ( isPrime x ) (cond (( = x 1 ) #t) (( = x 2 ) #t) ( else (isPrimeHelper x 2 ) ))) (define (printPrimesUpTo n) (define result '()) (define (helper x) (if (= x (+ 1 n)) result (if

Way to explode list into arguments?

拜拜、爱过 提交于 2020-01-04 16:25:32
问题 Assume I have a procedure upto that when called like (upto 1 10) will generate the list '(1 2 3 4 5 6 7 8 9 10) . If I want to use this list as arguments to a function like lcm that takes multiple arguments rather than a single list, like (lcm 1 2 3 4 5 6 7 8 9 10) is there a way to do this? 回答1: Use apply: e.g (apply lcm (upto 1 10)) "apply" applies a function to a list of arguments. 回答2: One way I found to do this using eval is the following: (eval (cons 'lcm (upto 1 10))) 回答3: In MIT

How to get the first, middle and last element of a list scheme and prolog?

家住魔仙堡 提交于 2020-01-04 13:54:09
问题 I am trying to write a function in Scheme and Prolog that returns first, middle and last item of a list. E.g., find([4,5,8,7,9],L), L = [4,8,9] . I came up with this piece of code for Scheme language, but I am new to Prolog and don't know much, so how I can get the same result in Prolog? (define (frst L) (car L)) (define (last L) (if (null? (cdr L)) (car L) (last (cdr L)))) (define (nth L x) (if (= x 1) (car L) (nth (cdr L) (- x 1)))) (define (firstMidLast L) (list (frst L) (nth L (ceiling (/

What's the return value of `define` in Scheme?

断了今生、忘了曾经 提交于 2020-01-04 13:39:59
问题 I'm curious about the return value of define in Scheme. So I wrote the following lines in Racket #lang r5rs (display (define a 3)) And get the error define: not allowed in an expression context in: (define a 3) I have 2 questions about this: Does it mean that define has no return value? According to R5RS, define is not an expression. It's a program structure. Is it true that only expressions have return values, and other forms don't? 回答1: "If a tree falls in a forest and no one is around to