scheme

How to define a variadic function

时光毁灭记忆、已成空白 提交于 2019-12-04 04:28:19
I'm looking for something similar to Javascript's arguments array: function parent(){ child.apply(this.arguments); } I'm aware of the dot notation for variable argument lengths and also scheme's apply function. This doesn't seem to work as the dot is taken to be the first argument: (define (parent .) (list .)) (parent 1 3 4 6 7) Error: bad argument count - received 5 but expected 1: #<procedure (array arg-list)> This works but isn't ideal. I'd like to call the function without the extra syntax to define the args list: (define (parent args-list) (apply list args-list)) (parent 1 3 4 6 7) Error:

Modification of the basic if expression in Scheme. Why does it go into an infinite loop?

冷暖自知 提交于 2019-12-04 04:22:44
问题 In Scheme, I modified the basic 'if' command as: (define (modified-if predicate then-clause else-clause) (if predicate then-clause else-clause)) And then I defined a simple factorial generating program using the modified version of if: (define (factorial n) (modified-if (= n 0) (* n (factorial (- n 1))))) Now, when I call the above function, it goes into an infinite loop. Why does that happen? 回答1: Scheme has eager evaluation. This means that, unless you're using a special form (like if ) or

using lambda instead of let in scheme

烂漫一生 提交于 2019-12-04 04:19:35
In SICP 1.2.1 there is a function that makes a rational number, as follow: (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) I'm just curious how you can implement the same thing using lambda instead of let, without calling GCD twice. I couldn't figure it out myself. Looking at SICP section 1.3.2 , (let ((<var1> <exp1>) (<var2> <exp2>) ... (<varn> <expn>)) <body>) is equivalent to ((lambda (<var1> ...<varn>) <body>) <exp1> ... <expn>) So your procedure, (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) should be equivalent to (define (make-rat n d) (

CLI shell script code generation from compiled executable? [closed]

家住魔仙堡 提交于 2019-12-04 03:41:41
Question, topic of discussion I am very interested in generation of command line shell scripting source code from code written in a more robustness-promoting, well-performant and platform-independent compiled language (OCaml, for instance). Basically, you would program in a compiled language to perform any interactions with the OS that you want (I would propose: the more complex interactions or ones that are not easy to do in a platform-independent way), and finally you would compile it to a native binary executable (preferably), which would generate a shell script that effects in the shell

Extensible macro definitions

↘锁芯ラ 提交于 2019-12-04 03:26:48
Inspired by a comment thread on a related question regarding functions instead of macros. Is there any way to extend a Scheme syntax definition so that it can use the previous definition of the syntax in the new definition? Furthermore, this must be extensible, that is, it must be possible to chain the technique together several times. For example, say we want to extend lambda so that every time a function defined with lambda is called, it prints "foo" before executing the function body. We can do this in the following way: (define-syntax old-lambda lambda) (define-syntax lambda (syntax-rules

Clojure or Scheme bayesian classification libraries?

好久不见. 提交于 2019-12-04 02:54:53
Any pointers to scheme/racket or clojure bayesian classification libraries? I need one for a toy/learning project that I'm going to do. dvogel For clojure there is Incanter. It's more than just a bayes library. It is more akin to R . The documentation has two sections about it's bayes capabilities: 1 , 2 . Weka is kind of classic. It is a Java lib, so it can be accessed from Clojure If you use Clojure, you have full access to Java libraries. Classifier4J seems to be a good fit, although development stopped several years ago. You should be able to find several more if you dig through

How do I get an item from a list at a given index in racket language?

青春壹個敷衍的年華 提交于 2019-12-04 02:31:08
I'm trying to get an item from a list at a given index for a loop statement. (define decision-tree-learning (lambda (examples attribs default) (cond [(empty? examples) default] [(same-classification? examples) (caar examples)] ; returns the classification [else (lambda () (let ((best (choose-attribute attributes examples)) (tree (make-tree best)) (m (majority-value examples)) (i 0) (countdown (length best)) ; starts at lengths and will decrease by 1 (let loop() (let example-sub ; here, totally stuck now ; more stuff (set! countdown (- countdown 1)) ; more stuff )))))]))) In this case, best is

Why does Scheme have both list and quote?

蓝咒 提交于 2019-12-04 01:25:07
Since (list 1 2 3) yields (1 2 3) and (quote (1 2 3)) yields (1 2 3), what is the rationale for having both? Since Scheme is otherwise so spare, these must have some meaningful difference. What is that? In the example you mentioned quote and list have the same result because numeric constants evaluate to themselves. If you use expressions that are not self-evaluating in the list (say variables or function calls), you'll see the difference: (quote (a b c)) will give you a list that contains the symbols a , b and c while (list a b c) will give you a list containing the values of the variables a

How can you rewrite “begin” in Scheme?

家住魔仙堡 提交于 2019-12-04 00:51:24
As the Wikipedia article explains, begin in Scheme is a library form that can be rewritten using more fundamental forms like lambda . But how do you rewrite a begin , especially considering the following? x ===> error: undefined identifier: x (begin (define x 28) x) ===> 28 x ===> 28 You cannot. The thing is that begin has two roles: one is sequence a bunch of side-effectful expressions, and the other is that it is used to "splice" macro results. The fact that you can use begin with a definition as in the above is a result of this second feature, and you cannot write it yourself. If you really

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

扶醉桌前 提交于 2019-12-04 00:20:34
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]) but I still would like to know if there is a way to get that variable from the outer scope (or if