scheme

representing sets with lambda functions

和自甴很熟 提交于 2019-12-22 17:47:08
问题 I am struggling with understanding what I really need to do, and would like some outside input or a point to a good reference. I have been asked to use procedural representation to "implement sets of numbers." Each set will be a one argument function that takes a number and decides if the number is in the set. A few functions (that I have read can be defined in one line) that I have to create: A function that returns a function taking a number as an argument and checks if the number is in the

Programatically filling in a letrec in Scheme. Macros or eval?

柔情痞子 提交于 2019-12-22 12:22:23
问题 I'm just playing with an NFA for string recognition. I have a macro that creates a function which consumes input and passes on the rest to some other functions. Because there might be loops in my NFA graph, I'm using letrec to put the whole thing together. Here is some code (been testing in PLT-Scheme): (define-syntax-rule (match chars next accepting) ; a function that consumes a list of chars from a list l. ; on success (if there's more to do) invokes each of next on the remainder of l.

Macro to use dot.notation to get structure fields in Racket

纵然是瞬间 提交于 2019-12-22 10:53:19
问题 For a structure and its instance defined as follows: (struct dog (name breed age)) (define mydog (dog "lassie" "collie" 5)) (example from https://learnxinyminutes.com/docs/racket/) The usual way to get a structure field is as follows: (dog-name mydog) ; => "lassie" Can there be a macro so that we can perform above using following dot notation: (mydog.name) ; => "lassie" Or, parentheses may not be needed: mydog.name ; => "lassie" Macro for dot notation is used on this page: http://www

Little Schemer: write function that only supports lists of length ≤ 2

扶醉桌前 提交于 2019-12-22 10:46:39
问题 In the book The little schemer , we find this function that only supports lists with length smaller than or equal to 1 : (((lambda (mk-length) ; A. (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l ) 0) (else (add1 ((mk-length eternity ) (cdr l)))))))) '(1)) I want to study step by step, and want to write the similar function that supports only lists of length smaller than or equal to 2 . Please, don't answer this question by offering code like: (((lambda (mk-length) ; B.

Scheme assignment

点点圈 提交于 2019-12-22 08:31:39
问题 When I evaluate the following expression every time I get the value 10. (((lambda (x) (lambda () (set! x (+ x 10)) x)) 0)) However I just modify by abstracting the above procedure with a name and call foo every time the value increments by 10!! (define foo ((lambda (x) (lambda () (set! x (+ x 10)) x)) 0)) Can anybody please explain this? 回答1: The function you are calling is a counter that returns a number 10 higher every time it's called. In the first case, every time, you are creating a new

Scheme Confusing of Let and Let*

微笑、不失礼 提交于 2019-12-22 07:56:15
问题 (let ((x 2) (y 3) (let ((x 7) (z (+ x y))) (* z x))) With the code above, why is the answer 35, not 70? In the second let , x is 7 so z should be 7 + 3 = 10, and then the result should be 7 * 10 = 70. I know got another is let* I am very confusing between this 2. The sample is grabs from google. I already google but just can't get it. 回答1: x is still bound to the outer let when calling (+ x y) . 回答2: To expand on Leppie's answer: if you had written (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y)))

Racket: Identifying tail recursion?

自古美人都是妖i 提交于 2019-12-22 07:18:08
问题 I wrote two different functions in racket to determine whether a list of numbers is ascending: (define (ascending list) (if (<= (length list) 1) #t (and (< (car list) (car (cdr list))) (ascending (cdr list))))) (define (ascending-tail list) (ascending-tail-helper #t list)) (define (ascending-tail-helper prevBool rest) (if (<= (length rest) 1) prevBool (ascending-tail-helper (and prevBool (< (car rest) (car (cdr rest)))) (cdr rest)))) I had the hardest time determining whether or not the first

Is it possible to “extend” a function / lambda / macro in Scheme?

最后都变了- 提交于 2019-12-22 07:12:08
问题 For example: if I want the function equal? recognize my own type or record, can I add a new behavior of equal? ? without erasing or overwriting the old one? Or for example if I want to make the function "+" accept also string? 回答1: Rather than using import , a better solution is to keep track of the original function by let -binding it. It's also better to check that the type of the argument is a string, rather than that it is not a number. Using both of these approaches means that it's

Is it possible to “extend” a function / lambda / macro in Scheme?

本秂侑毒 提交于 2019-12-22 07:10:02
问题 For example: if I want the function equal? recognize my own type or record, can I add a new behavior of equal? ? without erasing or overwriting the old one? Or for example if I want to make the function "+" accept also string? 回答1: Rather than using import , a better solution is to keep track of the original function by let -binding it. It's also better to check that the type of the argument is a string, rather than that it is not a number. Using both of these approaches means that it's

Drawing onto canvas% element

我是研究僧i 提交于 2019-12-22 06:56:01
问题 I have a problem while trying to draw onto a canvas GUI element. I create a frame, a canvas and try to draw on the dc context of the canvas with the draw-line method, but nothing happens. The frame with the canvas is shown, but the line isn't shown on the canvas. (require racket/gui/base) (define frame (new frame% [label "Frame"] [width 500] [height 500])) (define canvas (new canvas% [parent frame])) (define dc (send canvas get-dc)) (send dc draw-line 10 10 200 200) (send frame show #t) Does