racket

Writing an auto-memoizer in Scheme. Help with macro and a wrapper

落花浮王杯 提交于 2019-12-10 14:50:00
问题 I am facing a couple of problems while writing an auto-memoizer in Scheme. I have a working memoizer function, which creats a hash table and checks if the value is already computed. If it has been computed before then it returns the value else it calls the function. (define (memoizer fun) (let ((a-table (make-hash))) (λ(n) (define false-if-fail (λ() #f)) (let ((return-val (hash-ref a-table n false-if-fail))) (if return-val return-val (begin (hash-set! a-table n (fun n)) (hash-ref a-table n)))

Racket - how to get the “previous executed command” in bash script?

半城伤御伤魂 提交于 2019-12-10 14:43:09
问题 Is there any keyboard which echoes to the screen the "previously executed command"? Something like the up-arrow key at Linux bash... It's not duplicate to How do I get "previous executed command" in a bash script? 回答1: Load XREPL and you'll get readline-style input editing, which should include the ability to load previous lines using the up-arrow key. (require xrepl) 来源: https://stackoverflow.com/questions/15011939/racket-how-to-get-the-previous-executed-command-in-bash-script

For-each and map in Scheme

不想你离开。 提交于 2019-12-10 14:38:06
问题 Are there any difference between these 2 functions in scheme?I am using Dr Racket R5RS language to make a simulator game and I could not decide which one is better. 回答1: for-each evaluates the given function on the list elements left-to-right, and discards the return value of the function. It's ideal for doing side-effecting things to each element of the list. map evaluates the given function on the list elements in no specific order (though most implementations will use either right-to-left

Returning from a function inside when statement

巧了我就是萌 提交于 2019-12-10 14:23:24
问题 All I'm trying to do is use a when statement to return a value :( I want the functionality of: if(x) return y And I'm trying to use: (when (x) y) But the when statement is not evaluating in a way that exits the function and return y. It just happily carries on to the next line. Is there a way to do this without making an extremely ugly looking if-else block? mzscheme/racket does not allow 1-armed ifs. 回答1: You tagged this as both Common Lisp and Racket, which are two completely different

Using built-in math operators with custom struct

强颜欢笑 提交于 2019-12-10 13:44:17
问题 I want to be able to do something like this: (struct point (x y)) (define p1 (point 1 2)) (define p2 (point 10 20)) (+ p1 p2) ; -> (point 11 22) Is it possible to teach a struct like point to work with built-in math operators like + ? The docs seem to manage to implement custom (equal? ...) handling in section 5.5 on this page. What I'm trying to do is quite similar ... Or should I just define function like (point-add p1 p2) ? 回答1: You can either Go with point-add Use your own + that matches

Difference between eq? and = in Scheme?

不羁岁月 提交于 2019-12-10 13:40:35
问题 > (eq? 1 1) #t > (eq? 1.1 1.1) #f > (= 1.1 1.1) #t This is the interaction window in DrScheme. Could somebody please explain the difference between = and eq? in Scheme? 回答1: = compares numbers. eq? tests if the parameters represent the same data object in memory. eqv? should work in the second case as it tests same as eq? but tests primitives specially. More on equlivence predicates in scheme here. 回答2: I would guess that since eq? evaluates to #f unless its parameters represent the same data

Advantages of define over let

让人想犯罪 __ 提交于 2019-12-10 13:15:13
问题 During a conversation with Matt Flatt, one of the primary authors of Racket, I was told (in passing) that the let form is not recommended by the community and is largely being replaced by define . What are the advantages of define over let that would prompt the Racket community to elect to use it in place of let ? For reference, define and let from the Racket documentation are linked here. 回答1: "To reduce rightward drift" See the section 4.2 on definitions: http://www.ccs.neu.edu/home

What is the definition of “natural recursion”?

我的梦境 提交于 2019-12-10 12:43:24
问题 The Third Commandment of The Little Schemer states: When building a list, describe the first typical element, and then cons it onto the natural recursion. What is the exact definition of "natural recursion"? The reason why I am asking is because I am taking a class on programming language principles by Daniel Friedman and the following code is not considered "naturally recursive": (define (plus x y) (if (zero? y) x (plus (add1 x) (sub1 y)))) However, the following code is considered

Scheme, how do you append a list with a single item?

二次信任 提交于 2019-12-10 12:17:47
问题 I'm trying to make a function to append a list with a single item. What it's doing though is returning a dot pair. (define (append lst x) (cond ((null? lst) x) (else (cons (car lst) (append (cdr lst) x))))) The output I'm getting is > (append '(1 2 3 4 5 6 7) 8) (1 2 3 4 5 6 7 . 8) I'm trying to get (1 2 3 4 5 6 7 8) Thanks. 回答1: Try this: (define (append lst x) (cond ((null? lst) (cons x '())) ; here's the change (else (cons (car lst) (append (cdr lst) x))))) Notice that all proper lists

Implementation of Curried Functions in Scheme

℡╲_俬逩灬. 提交于 2019-12-10 10:29:21
问题 What happens when I do the following? (define ((func x) y) (if (zero? y) ((func x) 1) 12)) I understand that I can do this: (define curried (func 5)) And now I can use curried. What I'm curious about is in the definition of the function. Does the line ((func x) 1) create a new lambda with x as the argument, and then invoke it on 1? Or is it smarter than that and it just re-uses the existing one. (For example, if I do (curried 0) , the ((func x) 1) line would be equivalent to (curried 1) -