scheme

Spread a list into parent sexp

故事扮演 提交于 2019-12-10 19:45:24
问题 Is there a form in any lisp that could "spread" a list in the parent sexp? Like: (+ (spread '(1 2 3))) -> (+ 1 2 3) 回答1: There are two way to do it. Which is better depends on what you want in the end. Generally, you can use ,@ inside ` (backquote). The form following ,@ is evaluated to produce a list, which is then spliced into the template: * `(+ ,@'(1 2 3)) (+ 1 2 3) * (eval `(+ ,@'(1 2 3))) 6 Or, if you just want to call a function with its arguments which are packed in a list, it will be

Parameter Passing in Scheme

人盡茶涼 提交于 2019-12-10 19:32:33
问题 Can anyone help me out understanding the various parameter passing modes in Scheme? I know Scheme implements parameter passing by value. But how about other modes? Is there any good documentation for parameter passing in Scheme? 回答1: Scheme has only call-by-value function calls. There are other alternatives that can be implemented within the language, but if you're a beginner then it's best to not even try them at this point. If you're looking for a way to pass values "by reference" -- then

scheme tail recursion

本小妞迷上赌 提交于 2019-12-10 19:10:07
问题 I am trying to create a scheme tail recursive function flatten-tl-rec that flattens a nested list of lists. (define flatten-tl-rec (lambda (xs) (letrec ([flatten-tl-rec-acc (lambda (xs acc) (cond ((empty? xs) acc) ((list? (first xs)) (flatten-tl-rec-acc (rest xs) (append (flatten-tl-rec-acc (first xs) '()) acc))) (else (flatten-tl-rec-acc (rest xs) (cons (first xs) acc)))) )]) (flatten-tl-rec-acc xs '())))) (flatten-tl-rec '(1 2 3 (4 5 6) ((7 8 9) 10 (11 (12 13))))) But I am getting (13 12 11

decent way of nested definition in scheme

≡放荡痞女 提交于 2019-12-10 18:53:53
问题 I want to define a constant foo using an auxiliary function, say, bar . And I want to hide bar inside the definition of foo , so I come with this code: (define foo (define (bar n) (+ n n)) (bar 1)) However, this definition causes syntax errors in many scheme implementations(mit-scheme, racket, guile, etc.). I have three workarounds but none of them seems satisfactory: (define foo1 ((lambda () (define (bar n) (+ n n)) (bar 1)))) (define foo2 (let ((bar (lambda (n) (+ n n)))) (bar 1))) (define

Remove duplicates and sort a list

。_饼干妹妹 提交于 2019-12-10 18:41:03
问题 I am trying to write a procedure that takes a list that may or may not include duplicates, and then return that list without duplicates, and in sorted order. What I came up with so far is: (define (remove-duplicated list) (if (null? list) '() (if (= (car list) (cadr list)) (cdr list) (cons (car list) (remove-duplicates (cdr list)))))) I'm not quite sure what the problem is, besides sorting the list. For example, if I input (remove-duplicates '(3 3 4 5 6 6 7)) returns (3 4 5 6 6 7) 回答1: a

Append string to existing textfile in IronScheme

一笑奈何 提交于 2019-12-10 18:19:14
问题 We are trying to construct a log file using IronScheme, and we have written a code for it using racket. It works fine in racket, but IronScheme throws an error. This is what we have so far: (define write-to-log (lambda(whatToWrite) (with-output-to-file "robot-log.txt" (lambda () (printf (string-append whatToWrite "\r\n" ))) #:exists 'append))) See how we use the "exists" optional parameter when using with-output-to-file. We are not sure how to make this optional parameter work with IronScheme

Can the CPS-styled `call/cc` be written in terms of a hypothetical non-CPS-styled `cc'`?

牧云@^-^@ 提交于 2019-12-10 18:13:17
问题 In a language which supports continuation, e.g. Scheme, Ruby, and Haskell, suppose there is a function cc' which takes no argument and return the current continuation, so that the caller that obtains a continuation by calling cc' can then invoke the continuation anywhere and as often as it likes. cc' can be written in terms of the CPS-styled call/cc , by passing an identity function as an argument to call/cc . Conversely, can the CPS-styled call/cc be written in terms of the non-CPS-styled cc

Find the index of element in list

被刻印的时光 ゝ 提交于 2019-12-10 17:33:10
问题 I need to get index of element in list in scheme. For example: (... 2 '(2 3 4 5)) 0 (... 4 '(2 3 4 5)) 2 Can someone help? 回答1: Somthing like this (define list-index (lambda (e lst) (if (null? lst) -1 (if (eq? (car lst) e) 0 (if (= (list-index e (cdr lst)) -1) -1 (+ 1 (list-index e (cdr lst)))))))) 回答2: The following is the clearest solution I could come up with: (define (get-list-index l el) (if (null? l) -1 (if (= (car l) el) 0 (let ((result (get-list-index (cdr l) el))) (if (= result -1)

Identity function in Scheme

扶醉桌前 提交于 2019-12-10 17:24:14
问题 Is the identity function pre-defined in Scheme? 回答1: Yes, but it's called values. Actually, that's usually used for multiple values, so it's a generalized multiple-arity identity function. 回答2: In DrRacket I know that identity works. For example, (identity 12) => 12 来源: https://stackoverflow.com/questions/4969927/identity-function-in-scheme

How to use (read) correctly in mit-scheme?

青春壹個敷衍的年華 提交于 2019-12-10 17:21:13
问题 I read in the documentation and rosetta code that (read) is used to get input from the console. So I wrote this code to check this: (display (+ (read) 1)) But mit-scheme never asks for user input and the program just terminates. Why is this the case? 回答1: In the REPL, (display (+ (read) 1)) works as expected. When (display (+ (read) 1)) is placed in a source file, and the file is run as a script using mit-scheme --quiet < program.scm (reference), mit-scheme never asks for user input and the