racket

How to define basic Python structures?

﹥>﹥吖頭↗ 提交于 2019-12-11 07:13:15
问题 I'm coming to Python from Racket. In Racket, I would define a Point structure like this: (struct Point (x y) #:transparent) A point is now a structure with two fields named x and y . I can compare two structures for (deep) equality by calling equal? . What is the equivalent in Python? It looks to me like I have to write twelve lines: class Point(): def __init__(self,x,y): self.x = x; self.y = y; def __eq__(self, other): return ((type(other) is Point) and self.x == other.x and self.y == other

When to use define and when to use let in racket

寵の児 提交于 2019-12-11 06:47:00
问题 I'm learning racket and I have a question about when to use define and when to use let . I have this function: (define my-function (lambda (param1 param2 list1) (/ (count (lambda (x) (define a (param1 (remove x list1))) (define b (drop-right x 1)) (define c (param2 a x-sin-clase)) (eq? (last x) (last c))) (cdr list1)) (length (cdr list1))))) Without knowing what the above function does. Is it correct to use define inside the function body? I have read somewhere that define is used to declare

How to load a huge file into a String or list in Racket?

本秂侑毒 提交于 2019-12-11 06:18:41
问题 I have a HUGE file that I need to do operations on. Huge as in approx. half a million words. I just want to read it into a list or String so I can do things with it later. Also I know I could load it into a string using file->string OR use file->list, file->lines, but these seem to take waayy too long. Is this the right way to load it into a list?: (define my-list (with-input-from-file "myFile.txt" read)) Whenever I run my program I just get the first line printed out. Seems to work for

Regular expression for contents of parenthesis in Racket

纵饮孤独 提交于 2019-12-11 06:17:14
问题 How can I get contents of parenthesis in Racket? Contents may have more parenthesis. I tried: (regexp-match #rx"((.*))" "(check)") But the output has "(check)" three times rather than one: '("(check)" "(check)" "(check)") And I want only "check" and not "(check)". Edit: for nested parenthesis, the inner block should be returned. Hence (a (1 2) c) should return "a (1 2) c". 回答1: Parentheses are capturing and not matching.. so #rx"((.*))" makes two captures of everything. Thus: (regexp-match

The Order of Variable and Function Definitions

倾然丶 夕夏残阳落幕 提交于 2019-12-11 05:04:37
问题 Why is it that: Function definitions can use definitions defined after it while variable definitions can't. For example, a) the following code snippet is wrong: ; Must define function `f` before variable `a`. #lang racket (define a (f)) (define (f) 10) b) While the following snippet is right: ; Function `g` could be defined after function `f`. #lang racket (define (f) (g)) ; `g` is not defined yet (define (g) 10) c) Right too : ; Variable `a` could be defined after function `f` #lang racket

Hide extra separator lines between empty menu items

感情迁移 提交于 2019-12-11 04:43:44
问题 Separator lines still appear after hiding menu-items from this link => Can't hide "Preferences" item in edit-menu I have searched in racket documentation and found only adding new separator menu item. https://docs.racket-lang.org/gui/separator-menu-item_.html Is it possible to hide these extra lines between empty menu items? 回答1: Separator lines are created in the "between" methods. Those are removed by using void . #lang racket/gui (require framework) (define menu-super-frame% (frame

How to replace an element in a specific position of a list?

空扰寡人 提交于 2019-12-11 04:15:25
问题 I want to replace an element in a specific position of a list. I have this so far: (define alon (list 1 2 3 4 5 6 7 8 9 10)) (define pos (list 3 6 9)) ;; list with positions to be replaced (define insert (list "a" "b" "c")) ;;replacement (define (list-insert xalon xpos xins counter) (cond ((empty? xins) (cons (rest xalon) '())) ((= counter (first pos)) (cons (first xins) (list-insert (rest xalon) (rest xpos) (rest xins) (add1 counter)))) (else (cons (first xalon) (list-insert (rest xalon)

Macro for keyword and default values of function arguments in Racket

泄露秘密 提交于 2019-12-11 04:12:12
问题 Keyword and default arguments can be used in Racket functions as shown on this page: https://docs.racket-lang.org/guide/lambda.html (define greet (lambda (#:hi [hi "Hello"] given #:last [surname "Smith"]) (string-append hi ", " given " " surname))) > (greet "John") "Hello, John Smith" > (greet "Karl" #:last "Marx") "Hello, Karl Marx" > (greet "John" #:hi "Howdy") "Howdy, John Smith" > (greet "Karl" #:last "Marx" #:hi "Guten Tag") "Guten Tag, Karl Marx" Since Racket is said to be able to

“Buffering” data entry into online form in case of disconnection (Racket)

北城余情 提交于 2019-12-11 03:31:08
问题 My company has an existing framework for online medical data entry. We are now working with some doctors in China who are interested in using this framework, however they have some concerns. On the technical side, the online data entry forms are written in Racket and saved into a MySQL database on a server in Europe after entry. Their concerns are that in some hospitals, Internet connection might be unstable and thus doctors might lose data that was just entered into a form. So the question

Language Scheme: find the sum of proper divisors

会有一股神秘感。 提交于 2019-12-11 03:19:57
问题 I am wondering how to write a function calculating the sum of proper divisors of a integer greater than 1. (define (sum-of-proper-divisors n) (cond [(= n 1) 1] [(= 0 (remainder n (sub1 n))) (+ (remainder n (sub1 n)) (sum-of-proper-divisors (sub1 (sub1 n))))] [else (sum-of-proper-divisors (sub1 n))])) This is the code that I wrote, however, it does not work. It will never stop evaluating because it will always do n-1. And I don't know how to fix this. Also, there might be other problems. How