scheme

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

Map, Filter, Foldr in DrRacket/Scheme

狂风中的少年 提交于 2019-12-20 19:38:36
问题 Programming language: Scheme/DrRacket We're currently going over map , filter , and foldr in my comp sci class. I understand that all three can be used to create abstract functions, but I am honestly a little confused about the difference between the three and when I'd use each one. Anyone care to explain what each is used for and how they are different? Unfortunately my book is not very clear. 回答1: The basic idea is that all three are ways of applying some function to all the elements of a

Is there a Haskell/ML-like compiler to C?

寵の児 提交于 2019-12-20 10:21:04
问题 People have written games for the iPhone in Scheme. Because (some) Scheme-compilers compile down to C, it was easy to mix with Objective-C and integrate with XCode. I am aware of patches for Haskell and OCaml compilers to enable ARM/iOS-backends. But those appear unofficial and experimental/unstable. I prefer a static haskell/ML-type type-system over Scheme's dynamic typing. Is there a stable ML/SML/Haskell compiler which generates C-code so that it can be used in a similar way as Scheme

Fixing Lisp Syntax

核能气质少年 提交于 2019-12-20 09:56:03
问题 Being a newbie to Lisp I'm wondering if the Lisp syntax could be "fixed"? Some people say the syntax in Lisp is one of its biggest strengths. I don't quite understand this. Isn't it possible to replace "obvious" parentheses with a combination of white spaces, new lines and indenting? Just like in Python? It looks to me like parentheses are the most used characters in Lisp code. I'm wondering if that's true - but if it is, isn't this a suggestion, that there is some redundancy in the syntax?

Why all the lambdas in The Little Schemer?

让人想犯罪 __ 提交于 2019-12-20 09:54:16
问题 After learning a bit of Scheme from SICP, I started reading The Little Schemer (which I find quite entertaining) and am about one fourth done. I noticed that I can write many (most? all?) solutions without using lambda whereas The Little Schemer always uses them. For example, the very first definition is (define atom? (lambda (x) (and (not (pair? x)) (not (null? x))))) which, unless I am mistaken, can be written more simply as (define (atom? x) (and (not (pair? x)) (not (null? x)))) Am I

What is the difference between 1 and '1 in Lisp?

孤街浪徒 提交于 2019-12-20 09:14:24
问题 I had never really thought about whether a symbol could be a number in Lisp, so I played around with it today: > '1 1 > (+ '1 '1) 2 > (+ '1 1) 2 > (define a '1) > (+ a 1) 2 The above code is scheme, but it seems to be roughly the same in Common Lisp and Clojure as well. Is there any difference between 1 and quoted 1? 回答1: Well, they are in fact very different. '1 is however precisely the same as (quote 1) . (car ''x) evaluates to the symbol 'quote'. 1 is an S-expression, it's the external