racket

Can `match` in Racket have patterns with variables from an outer scope?

瘦欲@ 提交于 2019-12-21 07:14:04
问题 Consider the following example: #lang racket (match '(cat . doge) [`(,a . ,b) (match b [a #t] [_ #f])] [_ "Not a pair"]) This is what I might write if I wanted to match pairs where the head and tail are the same. This doesn't work though because the second a is bound as a new variable (and matches anything). Are there any pattern forms which allow me to use the previously bound a from the outer scope? I know this can be achieved in the following way (match* ('cat 'doge) [(a a) #t] [(_ _) #f])

Is there a shorthand way to update a specific struct field in racket?

喜夏-厌秋 提交于 2019-12-21 07:12:18
问题 Suppose I have a struct with many fields: (struct my-struct (f1 f2 f3 f4)) If I am to return a new struct with f2 updated, I have to rephrase every other fields: (define s (my-struct 1 2 3 4)) (my-struct (my-struct-f1 s) (do-something-on (my-struct-f2 s)) (my-struct-f3 s) (my-struct-f4 s)) Which is redundant and would be a source of bugs if I update the number of the fields or changed their orders. I really wonder if there's a such way I can update a specific field for a struct like: (my

How to get rid of duplicates in a list, but keep the order

泪湿孤枕 提交于 2019-12-21 04:44:08
问题 I am using Intermediate Student with Lambda in DrRacket, I was wondering how one would remove the duplicates in a list, while keeping the order. For example (remove-dup (list 2 5 4 5 1 2)) would produce (list 2 5 4 1) . So far, I have this: (define (remove-duplicates lst) (cond [(empty? lst) empty] [(member? (first lst) (rest lst)) (remove-duplicates (rest lst))] [else (cons (first lst) (remove-duplicates (rest lst)))])) , but there's a problem since it doesn't keep the order. Can someone

How do I read a web page in Racket?

烂漫一生 提交于 2019-12-21 04:38:04
问题 All of the information I can find online is about writing web servers, but there seems to be very little about functions useful for web clients. Ideally, I would like the function to look something like this: (website "http://www.google.com") And return a string containing the entire web page, but I would be happy with anything that works. 回答1: Here's a simple program that looks like it does what you want: #lang racket (require net/url) (port->bytes (get-pure-port (string->url "http://www

Scheme add element to the end of list [closed]

和自甴很熟 提交于 2019-12-21 04:28:51
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . How can you add an element to the end of a list (before the empty) when only cons, first, rest, empty? and cond recursions can be used 回答1: Think about

Tonumber function (tonumber ‘(one two three) --> 123

爱⌒轻易说出口 提交于 2019-12-20 06:48:23
问题 After the solution of how to spell a number in racket? (spellNum) ,now I am trying to write a function which is opposite of this function. i.e (tonumber ‘(one two three) --> 123 so far I have written this working code (define (symbol->digit n) (case n ('zero 0) ('one 1) ('two 2) ('three 3) ('four 4) ('five 5) ('six 6) ('seven 7) ('eight 8) ('nine 9) (else (error "unknown symbol:" n)))) (define (numlist n) (map symbol->digit n)) (numlist '(one two three)) From numlist, I got '(1 2 3). But to

Using car and cdr

流过昼夜 提交于 2019-12-20 06:28:47
问题 I am new to scheme and having a hard time with using car and cdr. I have an AST string literal in ast. (define ast '(program ((assign (var i int) (call (func getint void int) ())) (assign (var j int) (call (func getint void int) ())) (while (neq (var i int) (var j int)) ((if (gt (var i int) (var j int)) ((assign (var i int) (minus (var i int) (var j int)))) ((assign (var j int) (minus (var j int) (var i int))))))) (call (func putint int void) ((var i int))))) ) I know car returns the head of

Transform a natural number to a specific base and return it as a list

若如初见. 提交于 2019-12-20 06:12:33
问题 I want to show the result of my function as a list not as a number. My result is: (define lst (list )) (define (num->base n b) (if (zero? n) (append lst (list 0)) (append lst (list (+ (* 10 (num->base (quotient n b) b)) (modulo n b)))))) The next error appears: expected: number? given: '(0) argument position: 2nd other arguments...: 10 回答1: I think you have to rethink this problem. Appending results to a global variable is definitely not the way to go, let's try a different approach via tail

Is there a way to check if all elements of a list are contained in another list in racket?

假装没事ソ 提交于 2019-12-20 05:41:28
问题 I want a function that does something like this: >(function '(1 2 3 4) '(1 2 3 4 5)) #t When in this case is returning #t because all elements of the first list are contained in the second list. Is there a function that does that without having to worry about order? 回答1: So as far as I know there is no built in function for this, and I believe the shortest way to define such function would be something like this. (define (list-check l1 l2) (andmap (λ(x) (not (boolean? (memq x l2)))) l1)) 回答2:

How to order my accumulate variable in this case on Racket?

陌路散爱 提交于 2019-12-20 04:36:31
问题 I am coding with Racket for educational reasons. I was given a task in which I should create a function that, without filter, would receive a list as an input and return another list only with the even numbers of the first list. I came up with this recursive definition of an iterative process: (define (add-even lista) (define (iter lista accu) (cond ((null? lista) accu) ((even? (car lista)) (iter (cdr lista) (cons (car lista) accu))) (else (iter (cdr lista) accu)))) (iter lista empty)) It