scheme

Print adjacent duplicates of a list (scheme)

不问归期 提交于 2019-12-12 15:31:00
问题 I'm trying to create a function that returns the adjacent duplicates of a list, for example (dups '(1 2 1 1 1 4 4) should return the list (1 4). This is the code I came up with so far: (define (dups lst) (if (equal? (car lst)(car(cdr lst))) (cons(cdr lst) '()) (dups(cdr lst)))) This function doesn't return all the adjacent duplicates, it only returns the first adjacent duplicates! How can I fix it so that it returns all the adjacent duplicates of a list? Thank you. 回答1: Once your code finds a

What is the difference between defining a function inline or not?

天大地大妈咪最大 提交于 2019-12-12 14:00:51
问题 I'm working through the book Structure and implementation of computer programs and in one of the chapters there were some code used to calculate the factorial of a number: (define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count))) Earlier in the book I learned that I can define functions inline in another function like this: (define (factorial n) (define (fact-iter product

Value returned by a define expression in Scheme

风流意气都作罢 提交于 2019-12-12 13:43:12
问题 I ran this in MIT/GNU Scheme: (define x (+ 2 3)) The interpreter then prints: ;Value: x But according to my textbook, the value returned by a define expression should be undefined. Why did the interpreter print ";Value: x" then? 回答1: If the standard report does not specify a return or mentions that it is undefined an implementation is literally free to choose the value returned and it would be according to the standard. That also means you cannot depend on one implementations behaviour would

Racket macro to define functions given a repeated pattern

早过忘川 提交于 2019-12-12 13:17:50
问题 The problem is quite difficult to explain because I need to collect my thoughts, so bear with me. I've been able to reduce the problem to a minimal example for illustrative purposes. The example will not make any sense as to what this would be useful for, but I digress. Say I want to extend the racket language to write things that look like this: (define-something (['a] 'whatever) (['b 'c] 'whatever2)) Between the square brackets is a sequence of one or more symbols, followed by a sequence of

Add a character to a frequency list

余生长醉 提交于 2019-12-12 13:16:57
问题 I have a project about huffman coding, and I am stuck, I don't understand why my code is not working. This is the exercise: Write a function add1 which, given a character, adds 1 to its frequency in a frequency list. If the character is not yet in the list of frequencies, it is added. (add1 "e" '(("l" 1) ("e" 2) ("x" 1))) → (("l" 1) ("e" 3) ("x" 1)) (add1 "e" '(("a" 4) ("b" 3))) → (("a" 4) ("b" 3) ("e" 1)) What I wrote: (define add1 (lambda (c l) (if (null? l) '() (if (member? c l) (if (equal

Why is (begin) valid in Scheme?

試著忘記壹切 提交于 2019-12-12 12:17:15
问题 I have tested in Racket and Chez Scheme and found (begin) is acceptable while (define a (begin)) is not. For example with Racket I got > (begin) > (define a (begin)) ; stdin:56:10: begin: empty form not allowed And my question is why is (begin) allowed at all? Is there any specific reason/intuition for this? 回答1: The form begin has two purposes. 1. To sequence the evaluation of expressions 2. To "splice" sequences together (used by macros) The first one is what is used most often: (begin e0

Why not implement `let` in terms of lexically closed `define`?

时光毁灭记忆、已成空白 提交于 2019-12-12 12:09:03
问题 I have been working with lisp-family languages for several years and feel like I have a pretty good grasp on them. I'm now writing my own lisp (as is the fashion, of course), but almost entirely avoiding re-implementing the same patterns that Scheme, Common Lisp and friends have used. One particular thing that I always found odd was all the variants of let ( letrec , flet , labels , let* ...). Say in a "no legacy carried over from the past" implementation of a lisp, I'd like to just be able

Is everything a list in scheme?

守給你的承諾、 提交于 2019-12-12 12:08:54
问题 Along with the book "Simply Scheme" (Second Edition) i'm watching the "Computer Science 61A - Lectures" on youtube. On the lectures , the tutor uses Stk interpreter, but i'm using chicken scheme interpreter. In the first lecture he uses the "first" procedure which if it's called like : (first 'hello) it returns "h". On the book of "Simply Scheme" it has an example of how first can be implemented: (define (first sent) (car sent)) Which to my testing and understanding works if sent is a list .

Implementation of variadic map function in Scheme

笑着哭i 提交于 2019-12-12 11:33:58
问题 As you can see in the example below, map function in Scheme is variadic function. > (map (lambda (number1 number2) (+ number1 number2)) '(1 2 3 4) '(10 100 1000 10000)) '(11 102 1003 10004) I want to implement this variadic option, but I only succeeded to find the two arguments map implementation: (define (map f lst) (if (null? lst) '() (cons (f (car lst)) (map f (cdr lst))))) Can someone help me implement variadic map function? 回答1: In Scheme, you can write a variadic function as (lambda x .

Subset / Subsequence Recursive Procedure in Simply Scheme Lisp

别等时光非礼了梦想. 提交于 2019-12-12 11:28:35
问题 I am working my way through Simply Scheme in combination with the Summer 2011 CS3 Course from Berkley. I am struggling with my understanding of the subset / subsequence procedures. I understand the basic mechanics once I'm presented with the solution code, but am struggling to grasp the concepts enough to come up with the solution on my own. Could anyone point me in the direction of something that might help me understand it a little bit better? Or maybe explain it differently themselves?