scheme

How do I test whether a variable is defined before referencing it?

给你一囗甜甜゛ 提交于 2019-12-09 06:54:41
问题 I would like to be able to test whether a variable is defined, prior to accessing it. I like to have a global that specifies a "debug level". If debug level is 0, no extra output is given. When greater than 1, debug output is given, with more verbosity at greater numbers. I also would like to set it up so that the procedures would run, and assume a level 0, if I had not gotten around to defining it. Something like: (where defined? is the magic I don't know how to do? (if (and (defined? debug

Is there an equivalent to Lisp's “runtime” primitive in Scheme?

大憨熊 提交于 2019-12-09 05:04:30
问题 According to SICP section 1.2.6, exercise 1.22: Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds). I'm using DrScheme, where runtime doesn't seem to be available, so I'm looking for a good substitute. I found in the PLT-Scheme Reference that there is a current-milliseconds primitive. Does anyone know if there's a timer in Scheme with better resolution? 回答1:

How to use append-map in Racket (Scheme)

廉价感情. 提交于 2019-12-08 19:51:27
问题 I don't fully understand what the append-map command does in racket, nor do I understand how to use it and I'm having a pretty hard time finding some decently understandable documentation online for it. Could someone possibly demonstrate what exactly the command does and how it works? 回答1: The append-map procedure is useful for creating a single list out of a list of sublists after applying a procedure to each sublist. In other words, this code: (append-map proc lst) ... Is semantically

No-argument (and) returns t

我怕爱的太早我们不能终老 提交于 2019-12-08 19:16:09
问题 Both CL and Scheme define (and) to return t (or #t ) with no arguments. I'm trying to understand the rationale for this. My naive assumption is that an empty set is false, and passing in zero arguments feels like passing in nothing that can be true. Edit: clojure follows the same convention. I must be missing some basic Lisp assumption. 回答1: The empty product is 1. The reason is that 1 is a neutral element for * . If you have the product of 2 and 3 and then multiply by the product of nothing,

How do collector functions work in Scheme?

末鹿安然 提交于 2019-12-08 17:48:07
问题 I am having trouble understanding the use of collector functions in Scheme. I am using the book "The Little Schemer" (by Daniel P. Friedman and Matthias Felleisen). A comprehensive example with some explanation would help me massively. An example of a function using a collector function is the following snippet: (define identity (lambda (l col) (cond ((null? l) (col '())) (else (identity (cdr l) (lambda (newl) (col (cons (car l) newl)))))))) ... with an example call being (identity '(a b c)

Would like to swap elements in programming [closed]

筅森魡賤 提交于 2019-12-08 12:28:40
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am messing around in language and need some help. I would like to create a swap function that swaps the first with the second. So if (swap '(a b c d e g)) should return (b a d c e g). I dont want to store any values doing it. Is there a function or way to do it in scheme? I have no idea if I

Re-implementing lists with closures in scheme

ぐ巨炮叔叔 提交于 2019-12-08 07:06:29
问题 This is purely a curiosity question for me: So for fun, one can easily redefine cons, car, and cdr like so: (define cons+ (lambda (a b) (lambda (f) (f a b) ))) (define car+ (lambda (f) (f (lambda (a b) a)))) (define cdr+ (lambda (f) (f (lambda (a b) b)))) This creates the basic functionality of a list using closures. In this context, what would be the best way to define an empty list? 回答1: There's no particular special value that you need as the empty list; you just need something and then to

How can I convert a string into exact number in Scheme Lisp?

那年仲夏 提交于 2019-12-08 06:44:48
问题 For example, I have this string: "6119726089.12814713" If I do (string->number "6119726089.12814713") - using the SISC implementation the result is 6.119726089128147e9 - and in Guile implementation is 6119726089.128147 but I would like an exact number, like: 611972608912814713/100000000 without loss precision. I'd like a function like (string->exact) or something like this. NOTE: please fix my non-native English and remove this message. Thanks. 回答1: Use (string->number "#e6119726089.12814713"

Why isn't a function calling itself in its body considered recursive (vs iterative)?

浪尽此生 提交于 2019-12-08 06:35:15
问题 I am learning Scheme/Racket and am confused with recursion-vs-iteration concept. The specific question is: Write a function that sums up a list of numbers. Take the following code, for example: (define (func list) (define (dostuff list res) (if (empty? list) res (dostuff (cdr list) (+ (car list) res)))) (dostuff list 0)) According to the instructor, this is a iterative solution. But I don't understand why. dostuff is calling itself within its implementation, so doesn't that automatically make

Generating list of 2-lists in Scheme

此生再无相见时 提交于 2019-12-08 05:08:01
问题 (define cart-product (lambda (sos1 sos2) (if (null? sos1) '() (cons (cart-prod-sexpr (car sos1) sos2) (cart-product (cdr sos1) sos2))))) (define cart-prod-sexpr (lambda (s sos) (if (null? sos) '() (cons (list s (car sos)) (cart-prod-sexpr s (cdr sos)))))) Calling (cart-product '(q w) '(x y)) produces (((q x) (q y)) ((w x) (w y))) . How I can produce ((q x) (q y) (w x) (w y)) instead? 回答1: Untested. Note that the append-list procedure I defined actually returns a list ending in sos2 . That is