racket

Running SICP Pattern Matching Rule Based Substitution Code

烂漫一生 提交于 2019-11-30 05:16:34
I have found the code from this lesson online (http://groups.csail.mit.edu/mac/ftpdir/6.001-fall91/ps4/matcher-from-lecture.scm), and I am having a heck of a time trying to debug it. The code looks pretty comparable to what Sussman has written: ;;; Scheme code from the Pattern Matcher lecture ;; Pattern Matching and Simplification (define (match pattern expression dictionary) (cond ((eq? dictionary 'failed) 'failed) ((atom? pattern) (if (atom? expression) (if (eq? pattern expression) dictionary 'failed) 'failed)) ((arbitrary-constant? pattern) (if (constant? expression) (extend-dictionary

streams in racket

元气小坏坏 提交于 2019-11-30 03:28:09
问题 Can anyone help me better understand how to write a stream? I understand that a stream is an infinite sequence of values and the way I have learned programming them is a representing them as a thunk that when called produces a pair of (1) the first element in the sequence and (2) a thunk that represents the stream for the second-through-infinity elements For example: (define powers-of-two (letrec ([f (lambda (x) (cons x (lambda () (f (* x 2)))))]) (lambda () (f 2)))) I understand here that it

How to download and parse a csv file in Racket?

霸气de小男生 提交于 2019-11-30 03:17:28
问题 How do I download and parse a csv file in Racket? 回答1: Use get-pure-port to download the file, and use the Planet library (require (planet neil/csv)) to parse it. The following example downloads and parses a csv file containing data on the size of the various Galapagos islands and how many species were found on each island. #lang racket (require (planet neil/csv:1:=7) net/url) (define galapagos-url (string->url "http://www.stat.washington.edu/~handcock/536/Data/galapagos.csv")) (define make

Programming Scheme(Racket) with VIM - How to get started

依然范特西╮ 提交于 2019-11-30 00:01:21
recently, I started programming Racket (formerly Scheme) in DrRacket. I quite fast I began to miss all the features of VIM in DrRacket, so I would like to use VIM for my scheme(racket) programming. I know that Emacs might be the best choice for intense lisp programming, but all I want is write a scheme(racket) file check syntax and then run it. Unfortunately, I could not figure out, how to invoke "racket" in the commandline on a file to get it doing the same as DrRacket. I am running Ubuntu 10.10 Maverick Meerkat, VIM 7.3 and I downloaded and installed Racket from the official website. Help to

What exactly is a “continuation prompt?”

北慕城南 提交于 2019-11-29 20:11:56
I'm trying to decipher the documentation call-with-continuation-prompt Applies proc to the given arg s with the current continuation extended by a prompt. The prompt is tagged by prompt-tag , which must be a result from either default-continuation-prompt-tag (the default) or make-continuation-prompt-tag . The result of proc is the result of the call-with-continuation-prompt call. I understand the part where it says "Applies proc to the given arg s with the current continuation" and then it's just gibberish from there. What does it even mean for a continuation to be "extended," and how does a

mcons in dr racket

那年仲夏 提交于 2019-11-29 18:15:07
问题 I'm having trouble reading output from dr racket. By default it displays lists using mcons. For example, sicp exercise 2.32 produces: > (subsets (list 1 2 3)) (mcons (mcons '() (mcons (mcons 3 '()) (mcons (mcons 2 '()) (mcons (mcons 2 (mcons 3 '())) (mcons (mcons 1 '()) (mcons (mcons 1 (mcons 3 '())) (mcons (mcons 1 (mcons 2 '())) (mcons (mcons 1 (mcons 2 (mcons 3 '()))) '())))))))) '()) I'm having trouble reading this. Is there a way to make the output look like: (() (3) (2) (2 3) (1) (1 3)

arbitrary precision addition using lists of digits

妖精的绣舞 提交于 2019-11-29 17:57:19
What I'm trying to do is take two lists and add them together like each list is a whole number. (define (reverse lst) (if (null? lst) '() (append (reverse (cdr lst)) (list (car lst))))) (define (apa-add l1 l2) (define (apa-add-help l1 l2) (cond ((and (null? l1) (null? l2)) '()) ((null? l1) (list (+ (apa-add-help '() (cdr l2))))) ((null? l2) (list (+ (apa-add-help (cdr l1) '())))) ((>= (+ (car l1) (car l2)) 10) (append (apa-add-help (cdr l1) (cdr l2)) (list (quotient (+ (car l1) (car l2)) 10)) (list (modulo (+ (car l1) (car l2)) 10)))) ;this is a problem (else (append (apa-add-help (cdr l1)

Counting elements of a list and sublists

霸气de小男生 提交于 2019-11-29 17:00:58
I'm trying to create a function to count all the elements in a list, including the elements of its sublists. initially, to get started, i came up with a basic function myList : (define myLength (lambda (L) (cond ((null? L) 0) (else (+ 1 (myLength (cdr L))))))) However, it doesn't help me account for function calls like: (numAtoms '()) "...should be 0" (numAtoms '(())) "...should be 0" (numAtoms '(1 1)) "...should be 2" (numAtoms '(1 (1 1) 1)) "...should be 4" (numAtoms '(1 (1 (1 1)) 1)) "...should be 5" I'm trying to use basic functions like length , null? , and list? . I think the trick here

Finding path between 2 points in Racket

∥☆過路亽.° 提交于 2019-11-29 16:54:22
I have following list of connections: (define routelist (list (list'a 'b) (list'a 'c) (list'b 'e) (list'b 'f) (list'b 'c) (list'a 'd) (list'e 'f) (list'f 'g))) Routes between 'a and 'g are to be found. This page shows a solution in Prolog: http://www.anselm.edu/homepage/mmalita/culpro/graf1.html I could manage following solution, though it is iterative: (define (mainpath routelist start end (outl '())) (if (equal? start end) (println "Same start and end points.") (for ((item routelist)) (when (equal? start (list-ref item 0)) (set! outl (cons start outl)) (if (equal? end (list-ref item 1))

How to compute the number of times pattern in one list appears in other list in Scheme

北慕城南 提交于 2019-11-29 16:44:31
I am stuck up in a Scheme program for about 5 hours. The program that I am working on should take two lists as input and then compute the number of times the pattern within the first list appears on the second list. For example : > (patt '(b c) '(a b c d e b c)) ==> answer = 2 (patt '(a b c) '(a b c a b c d e a b c c c)) ==> answer = 3 (patt '((a b) c) '(a b (a b) c d e b c)) ==> answer = 1 Below is the code that I have till now. (define (patt lis1 lis2) (cond ((null? lis1) 0) ((null? lis2) 0) [(and (> (length lis1) 1) (eq? (car lis1) (car lis2))) (patt (cdr lis1) (cdr lis2))] ((eq? (car lis1)