racket

How to split a list into two parts in Scheme

笑着哭i 提交于 2019-12-11 03:04:07
问题 Example: (split '(1 2 3 4) '3) the Answer should be: ((1 2 3) 4) The function required 1 list and 1 number, the output should be nested list the nested list consist of all elements of "mylist" which are equal or less than the "num", and the greater number should be on the right of the list. I tried but out put is only one list: (define (split mylist num) (cond ((null? mylist)'()) ((list? (car mylist))(split(car mylist) num)) ((> (car mylist) num)(split(cdr mylist) num)) (else(cons (car mylist

How to control order of Scheme macro expansion?

夙愿已清 提交于 2019-12-11 02:35:57
问题 I'm working with the Racket macro extension syntax-id-rules , that some other Scheme implementations provide under the name identifier-syntax . These let you specify macro expansions that will happen even when the defined identifier isn't in head position. So for example: (define hidden #f) (define-syntax proxy (syntax-id-rules (set!) [(set! proxy v) (set! hidden v)] [proxy hidden])) will set up the identifier proxy to be a proxy for hidden . This is a useless example, but it illustrates the

How to include another file of definitions in current Racket file?

喜欢而已 提交于 2019-12-11 02:26:51
问题 I've tried: #lang racket (require foo.rkt) and #lang racket (require "foo.rkt") and #lang racket (require (relative-in "." foo.rkt)) If a straightforward example to just include one file in the current file's directory exists in the documentation, I cannot find it. Please help. 回答1: Your second guess is actually correct: #lang racket (require "foo.rkt") However, what you need to do is also provide the functions you would like to require from the other file, otherwise no variables from foo.rkt

merging two lists positionally in Scheme

…衆ロ難τιáo~ 提交于 2019-12-11 02:20:58
问题 Example First list: (1 2 3 4) Second list: (* slot (- slot (/ slot slot))) Output: (* 1 (- 2 (/ 3 4))) The first list's elements will be inserted to the second list. The symbol slot in second list means the position for insertion. My Solution I have written a code snippet and it works for the above example. (define (insert-slot numbers slots) (cond [(null? slots) '()] ;; operate on the nested list and come back to the element after [(pair? (car slots)) (cons (insert-slot numbers (car slots))

Racket's Big Bang framework - making a game go faster using tick rate?

蹲街弑〆低调 提交于 2019-12-11 01:53:35
问题 So in the Big Bang framework you can specify a tick rate: (on-tick tick-handler tick-rate) This tick-rate value is evaluated only once I believe (there is a similar question on here where this is stated as so). If that's the case, how would one go about writing a game (such as Tetris) where you want the shape to drop down at a quicker rate as time goes by? I've got the shape-drop occurring on the tick-handler at present. If the tick-rate changed, the drop would occur more frequently. As it is

SCHEME recursion perfect number (beginner, hopefully easy fix)

北城余情 提交于 2019-12-11 01:49:04
问题 having an issue with my perfect number function. The objective of the code is to determine if the number is a perfect number, meaning it is equal to the sum of its divisors. Ex:6. Im having trouble with my code. Here's my function: (define (is-perfect x) (define (divides a b) (= (modulo b a) 0)) (define (sum-proper-divisors y) (if (= y 1) 1 (if (divides y x) (+ y (sum-proper-divisors (- y 1))) (if (= x 1) #f (= (sum-proper-divisors (- x 1) x))))))) 回答1: You almost got it! there are a couple

functions and lists in scheme/racket

人盡茶涼 提交于 2019-12-11 00:43:54
问题 How would you define a function which takes one argument, which should be a list, and returns the elements in the list which are themselves lists? (check-expect (find-sublists ’(1 2 () (3) (a b c) a b c)) ’(() (3) (a b c))) 回答1: Do you have experience designing functions that can filter through a list? A simpler problem with the same flavor as the original is something like this: design a function that takes a list of numbers and keeps only the even numbers. Would you be able to do that

add1 function from scheme to R5RS

自古美人都是妖i 提交于 2019-12-10 23:25:13
问题 I've written some code but it's not working because the add1 function I used in Scheme is not working with R5RS. What can replace add1 in R5RS? 回答1: The procedure add1 is very simple, you can implement it yourself: (define (add1 x) (+ x 1)) 回答2: late answer but an alternative is (making use of lambdas) .. (define add1 (lambda (x) (+ x 1))) 来源: https://stackoverflow.com/questions/13292094/add1-function-from-scheme-to-r5rs

Detecting the caller of a function in Scheme or Racket

拈花ヽ惹草 提交于 2019-12-10 22:36:19
问题 In Scheme or Racket is it possible to detect the caller of a function? For example, I can write a function to test if a list is a list of atoms as follows: (define atom? (lambda (x) (and (not (pair? x)) (not (empty? x))))) (define lat? (lambda (l) (define latt? (lambda (l) (cond ((null? l) #t) ((atom? (car l)) (latt? (cdr l))) (else #f)))) (if (null? l) #f (latt? l)))) but instead of the above, is there a function like "called-by" to do something like this: (define lat? (lambda (l) (cond (

Scheme - Optional arguments and default values

不想你离开。 提交于 2019-12-10 21:10:09
问题 I'm currently looking into Scheme, and the way I have understood it, procedures can take an arbitrary number of arguments. I have been trying to play around with this, but I'm struggling to grasp the concept. For instance, say I want to write a welcome message, based on information provided by the user. If user provides a first and last name, the program shout write: Welcome, <FIRST> <LAST>! ;; <FIRST> = "Julius", <LAST>= "Caesar" Welcome, Julius Caesar! Otherwise, the program should refer to