scheme

What are improper lists for?

元气小坏坏 提交于 2019-12-12 11:05:42
问题 This is a follow-up of my previous question: Why do we need nil? Clearly, proper lists are used most of the time. But what is the purpose of an improper list? 回答1: For no good reason. The only thing that improper lists are truly good for is as part of the syntax for association lists—and even there, a custom syntax for key-value pairs would be better. Any use you can think of for improper lists can be better implemented with record types—which, after all, subsume lists: you can define Lisp

Multiply without + or *

丶灬走出姿态 提交于 2019-12-12 10:52:54
问题 I'm working my way through How to Design Programs on my own. I haven't quite grasped complex linear recursion, so I need a little help. The problem: Define multiply , which consumes two natural numbers, n and x , and produces n * x without using Scheme's * . Eliminate + from this definition, too. Straightforward with the + sign: (define (multiply n m) (cond [(zero? m) 0] [else (+ n (multiply n (sub1 m)))])) (= (multiply 3 3) 9) I know to use add1 , but I can't it the recursion right. Thanks.

Scheme built-in to check list containment

[亡魂溺海] 提交于 2019-12-12 10:49:49
问题 In Python I can do "x in list" to see if the list contains x. Is there any equivalent built-in in Scheme to do this? 回答1: The R5RS, and the R6RS standard library for lists define memq , memv , and member which can be used for that purpose. 回答2: In PLT Scheme, one has (member whatever list) (memv whatever list) (memq whatever list) from the SRFI which use, respectively, equal? , eqv? , and eq? to test for equality. There are also a number of other library functions related to searching in

Counting unique elements in Scheme

痞子三分冷 提交于 2019-12-12 10:09:32
问题 Write a recursive Scheme procedure count-dist elements that takes a list with duplicate elements and returns the number of distinct elements in the list. This is my code, but it is not working correctly. Please help! Thanks!! (define (count-dist-elements lis) (cond ((null? lis) 0) ((null? (cdr lis))0) ((member (car lis)(cdr lis))) (else(+ 1(count-dist-elements (cdr lis)))))) p/s: let it be (count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9)) 回答1: It looks like you're getting pretty

Simplest example of need for “unification” in type inference

元气小坏坏 提交于 2019-12-12 09:05:17
问题 I'm trying to get my head around how type inference is implemented. In particularly, I don't quite see where/why the heavy lifting of "unification" comes into play. I'll give an example in "pseudo C#" to help clarify: The naive way to do it would be something like this: Suppose you "parse" your program into an expression tree such that it can be executed with: interface IEnvironment { object lookup(string name); } interface IExpression { // Evaluate this program in this environment object

How to 'display' multiple parameters in R5RS Scheme

╄→尐↘猪︶ㄣ 提交于 2019-12-12 08:59:23
问题 In R5RS Scheme how do you display multiple parameters, with a single call? my implementation below works, but adds extra parentheses and spaces. #!/usr/bin/env racket #lang r5rs (define (display-all . rest) (display rest)) (display-all "I " "have " "a " "lovely " "bunch " "of " "coconuts\n") results in owner@K53TA:~$ ./Template.ss (I have a lovely bunch of coconuts ) 回答1: Simplest: (define (display-all . vs) (for-each display vs)) Note the use of for-each instead of map - for-each is the same

Good simple algorithm for generating necklaces in Scheme?

自闭症网瘾萝莉.ら 提交于 2019-12-12 08:48:31
问题 A k-ary necklace of length n is an ordered list of length n whose items are drawn from an alphabet of length k, which is the lexicographically first list in a sort of all lists sharing an ordering under rotation. Example: (1 2 3) and (1 3 2) are the necklaces of length 3 from the alphabet {1 2 3}. More info: http://en.wikipedia.org/wiki/Necklace_(combinatorics) I'd like to generate these in Scheme (or a Lisp of your choice). I've found some papers... Savage - A New Algorithm for Generating

How does `let` work in Scheme?

試著忘記壹切 提交于 2019-12-12 08:48:11
问题 I use let to create a temporary variable, and then use this temporary variable in the next statement. However, DrScheme complained, let: bad syntax (not an identifier and expression for a binding) in: temp This is my code snippet: (define (case-one-helper str) (let (temp (substring str (+ 3 (string-contains str "my")))) (substring temp (string-contains temp " ")))) I wonder if the value of variable created by let has to be known at compiled time? Edit I've just figured out, missing () .

Help explaining how `cons` in Scheme work?

﹥>﹥吖頭↗ 提交于 2019-12-12 07:14:34
问题 This is the function that removes the last element of the list. (define (remove-last ll) (if (null? (cdr ll)) '() (cons (car ll) (remove-last (cdr ll))))) So from my understanding if we cons a list (eg. a b c with an empty list, i.e. '() , we should get a b c . However, testing in interaction windows (DrScheme), the result was: If (cons '() '(a b c)) (() a b c) If (cons '(a b c) '()) ((a b c)) I'm like what the heck :(! Then I came back to my problem, remove all elements which have adjacent

why am I getting “application not a procedure”?

久未见 提交于 2019-12-12 06:58:49
问题 I am trying to write a procedure that computes f by means of an iteratve process. The function f is defined by the rule that f(n) = n, if n < 4 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) + 4f(n - 4), if n >= 4. Here is my procedure: (define (f n) (define (newF temp n) (letrec ((first (- n 1)) (second (- n 2)) (third/fourth (- n 3)) (fifth (- n 4))) (define (d) ((if (< first 4) (set! temp (+ temp first)) (newF temp first)) (if (< second 4) (set! temp (+ temp (* second 2))) (newF temp second))