racket

Racket, split a list in two different size lists. And randomly

无人久伴 提交于 2019-12-12 04:45:31
问题 I would like to code a program that given a list and a percentage, splits the list in two different size lists. It should have random pick of the elements, that way the created lists are always different. These code is able to do that: (define (clamp x a b) (max (min x b) a)) (define (split pct xs) (define pos (exact-round (* (clamp pct 0.0 1.0) (length xs)))) (split-at (shuffle xs) pos)) Here is an example: (split 0.25 '(1 2 3 4 5 6 7 8 9)) '(6 2) '(3 7 1 4 5 8 9) But, instead of "shuffle" I

higher order procedure to print alternating pictures

江枫思渺然 提交于 2019-12-12 04:35:46
问题 definition of task : i have to make pumpkins and fishes hanging on a string terms used : what-is-it? ==>a function that determines whether to make a fish or a pumpkin fish-squared ==> a function to make a fish using 2 parameters pumpkin ==> a function to make a pumpkin with also 2 parameters decorations ==> a function that appends all the images together hang-by-thread ==> a function that hangs all the images to a thread extra for this exercise i have to use" (if (odd? k) fish-square pumpkin)

How to return exit status and output of system commands in Racket?

匆匆过客 提交于 2019-12-12 04:35:38
问题 I'd like to not only capture the output of a command like with (with-output-to-string (lambda () (system "ls -la"))) But also would like to be able to access the exit code, so that I do not have to parse the output to know whether the command was successful or not and can react on it accordingly. How do I do this in Racket? I found the documentation about subprocess, but I don't know how to provide all the arguments like standard out. I'd like to see some comprehensive example, in which the

scheme structures and lists

匆匆过客 提交于 2019-12-12 04:35:22
问题 (define-struct position (name numshares share-price)) (define p1 (cons (make-position "INT" 10 192) (cons (make-position "SSS" 4 42) empty))) mult is my helper function (define (mult n) ( * (position-numshares n) (position-share-price n))) const takes the position-numshares and the position-share-price in a list and multiplies them together. (define (const n) (cond [(empty? n) empty] [(cons? n) (+ (mult (first n)) )])) What I would like to do is take the first of the list and add the rest of

scheme how do you sum numbers in a list when you have structures and list of lists

流过昼夜 提交于 2019-12-12 04:34:10
问题 ;; An ATOM is one of: ;; -- Symbol ;; -- String ;; -- Number ;; An SEXP (S-expression) is one of: ;; -- empty ;; -- (cons ATOM SEXP) ;; -- (cons SEXP SEXP) So i'm trying to sum up all the numbers in SEXP! Here's my code, ;; sum-numbers: sexp -> Number (define (sum-numbers sexp) (cond [(empty? sexp) 0] [(ATOM? (first sexp)) (+ (atom-sum-numbers (first sexp)) (sum-numbers (rest sexp)))] [(SEXP? (first sexp)) (+ (sum-numbers (first sexp)) (sum-numbers (rest sexp)))])) ;; atom-sum-numbers: Atom -

.Dynamically changing the clock tick rate in big-bang

你离开我真会死。 提交于 2019-12-12 04:17:37
问题 The on-tick clause includes an option to change the clock tick rate. I have included my code to dynamically change the value depending on the world state value. The code is not working and I can't understand why. Another issue - how are world programs debugged? The "step" option doesn't work. ; physical constants (define HEIGHT 300) (define WIDTH 100) (define YDELTA 3) ; graphical constants (define BACKG (empty-scene WIDTH HEIGHT)) (define ROCKET (rectangle 5 30 "solid" "red")) (define ROCKET

SCHEME Mutable Functions

别来无恙 提交于 2019-12-12 03:52:51
问题 I've been self-teaching myself Scheme R5RS for the past few months and have just started learning about mutable functions. I've did a couple of functions like this, but seem to find my mistake for this one. (define (lst-functions) (let ((lst '())) (define (sum lst) (cond ((null? lst) 0) (else (+ (car lst) (sum (cdr lst)))))) (define (length? lst) (cond ((null? lst) 0) (else (+ 1 (length? (cdr lst)))))) (define (average) (/ (sum lst) (length? lst))) (define (insert x) (set! lst (cons x lst)))

(SCHEME): Dividing a bigint into tens, hundreds, ect. And into a list that contains english

纵饮孤独 提交于 2019-12-12 02:49:30
问题 So I'm having a hard time trying to write this program. The scope is a program that will take a large number, (say 1,000,000) and split it into its digits (ie 1,500,310 -> 1 million 500 thousand 3 hundred 1 ten 0 one). #lang r5rs (define (three_names x) (let loop ((x x) (myList '())) (if (< x 10) (cons x myList) (loop (quotient x 10) (cons (remainder x 10) myList))))) I've gotten it so that it will loop and return these values each into a list with some help from stackoverflow. (i.e. this

find sum of the squares of the digits of a number in scheme

a 夏天 提交于 2019-12-12 02:29:31
问题 I need to write a function in scheme which calculates the sum of square digits. ex - (sum-of-digits 130) > 10 This is my function. (define (sum-of-digits x) (if (= x 0) 0 (+ (modulo x 10) (sum-of-digits (/ (- x (modulo x 10)) 10))))) it doesn't work for some numbers. When I entered (sum-of-digits 130) , it returns 4. How can i fix this ? Also I need to use this function to find the stop numbers which are 0,1,4,16,20,37,42,58,89,145 ex :- (stop? 42)  #t (stop? 31)  #f How can I do this using

Why we can define a function with the same name of a built-in function Racket?

十年热恋 提交于 2019-12-12 01:36:28
问题 We can define a new function like this: (define (car x y) (+ x y)) And use car as an add function. Meanwhile, we lost the built-in function car . Why does Racket allow this? How could we recover the lost built-in function, here is car . 回答1: Definitions affect the current module only (and, if you export your definition, then any other modules that import your module). You can always import Racket's built-in functions under a different name, if you want to use car in your module for something