racket

How to split list into evenly sized chunks in Racket (Scheme)?

删除回忆录丶 提交于 2019-12-04 08:38:59
Example: How to convert list: '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) Into list of lists: '((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15)) Based on answers provided here so far, this is what I've come up with: First define function to take up to 'n' elements from beginning of the list: (define (take-up-to n xs) (define (iter xs n taken) (cond [(or (zero? n) (empty? xs)) (reverse taken)] [else (iter (cdr xs) (- n 1) (cons (car xs) taken))])) (iter xs n '())) Second is similar function for the rest of list: (define (drop-up-to n xs) (define (iter xs n taken) (cond [(or (zero? n) (empty? xs)) xs

Applying A List of Functions to a Number

爷,独闯天下 提交于 2019-12-04 08:26:34
I understand that functions in Scheme/Racket like map, foldr, and filter, can do wonderful things like apply a function to a list of elements. Is it possible to apply a list of functions to a single element? I would like to generate the values produced by each of the functions, then find their maximum. Thank you. For the first part, this procedure will apply a list of functions to a single argument, assuming that all the functions receive only one argument. A list with the results is returned (define (apply-function-list flist element) (map (lambda (f) (f element)) flist)) For the second part,

Anonymous lambdas directly referring to themselves

早过忘川 提交于 2019-12-04 07:51:23
Does Scheme or do any dialects of scheme have a kind of "self" operator so that anonymous lambdas can recur on themselves without doing something like a Y-combinator or being named in a letrec etc. Something like: (lambda (n) (cond ((= n 0) 1) (else (* n (self (- n 1))))))) No. The trouble with the "current lambda" approach is that Scheme has many hidden lambdas. For example: All the let forms (including let* , letrec , and named let ) do (which expands to a named let ) delay , lazy , receive , etc. To require the programmer to know what the innermost lambda is would break encapsulation, in

How write a function that returns a list of 'n' functions based on an input

China☆狼群 提交于 2019-12-04 06:46:53
问题 I'm looking to create a function that returns a list of 'n' functions each of which increments the input by 1, 2, 3... n respectively. I use DrRacket to try this out. A sample of expected outcome : > (map (lambda (f) (f 20)) (func-list 5)) (21 22 23 24 25) I'm able to write this down in a static-way : > (define (func-list num) > (list (lambda (x) (+ x 1)) (lambda (x) (+ x 2)) (lambda (x) (+ x 3)) (lambda (x) (+ x 4)) (lambda (x) (+ x 5))) [Edit] Also that a few restrictions are placed on

Scheme removing nested duplicates

霸气de小男生 提交于 2019-12-04 06:36:17
问题 So I'm programming in scheme and made a function that removes duplicated but it doesn't work for nested. I can't really figure out a good way to do this, is there a way to modify the current code I have and simply make it work with nested? lists? Here's my code (define (duplicates L) (cond ((null? L) '()) ((member (car L) (cdr L)) (duplicates (cdr L))) (else (cons (car L) (duplicates (cdr L)))))) 回答1: So your procedure jumps over elements that exist in the rest of the list so that (duplicates

(define (average …)) in Lisp

蓝咒 提交于 2019-12-04 05:50:40
I'm just playing around with scheme/lisp and was thinking about how I would right my own definition of average . I'm not sure how to do some things that I think are required though. define a procedure that takes an arbitrary number of arguments count those arguments pass the argument list to (+) to sum them together Does someone have an example of defining average ? I don't seem to know enough about LISP to form a web search that gets back the results I'm looking for. The definition would be a very simple one-liner, but without spoiling it, you should look into: a "rest" argument -- this

“Missing End Time” with Google Calendar and Racket Google Package

谁都会走 提交于 2019-12-04 05:29:09
问题 I am using a Google API library for Racket to try to update a Google Calendar. (The API is incomplete so I'm extending it as I go.) I seem to have trouble with adding events to a calendar using the events.insert put call. The code that does the put looks like: (define (insert-events calendar-id event #:token [t #f] #:max-attendees [max-attendees #f] #:send-notifications [send-notifications #f] #:supports-attachments [supports-attachments #f]) (json-api-post (string->url (format "https://www

How to wrap input text in text-field using the Racket GUI plugin

巧了我就是萌 提交于 2019-12-04 05:19:56
问题 Just need a basic example of a text-field% that wraps input (define blogPost% (class horizontal-panel% (super-new) (define (callback button event) (define title-new-value (send titleoutput get-value)) (define new-value (send output get-value)) (save title-new-value new-value)) ;;(display title-new-value) ;;(display new-value)) (define button (new button% (label "Submit") (vert-margin 0) (parent this) (callback callback))) (define titleoutput (new text-field% (label " title") (min-height 20)

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

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: