scheme

How can I filter null values from this list?

你离开我真会死。 提交于 2019-12-14 03:23:21
问题 I have the following procedure for creating all prime-pairs in a list: (define (prime-pairs lst) (define (split lst pos) (list (drop-right lst pos) (take-right lst pos))) (define (prime-pairs-iter n acc) (cond ((= n 0) (filter (lambda (e) (not (null? e))) acc)) (else (prime-pairs-iter (- n 1) (let ((s (split lst n))) (if (and (prime? (list->number (car s))) (prime? (list->number (cadr s)))) (append s acc) acc)))))) (prime-pairs-iter (- (length lst) 1) '())) (Full code: https://gist.github.com

How to have ball collide with bricks in Breakout (racket)

◇◆丶佛笑我妖孽 提交于 2019-12-14 02:59:55
问题 I've been trying to get Breakout to work in Racket, so far the ball bounces from the paddle (paddle is controlled by mouse) and the bricks are present Here is the full on code: (require 2htdp/image) (require 2htdp/universe) (define WIDTH 400) (define HEIGHT 400) (define BALL-RADIUS 10) (define BALL-IMG (circle BALL-RADIUS "solid" "red")) (define REC-WIDTH 50) (define REC-HEIGHT 10) (define REC-IMG (rectangle REC-WIDTH REC-HEIGHT "solid" "grey")) (define BRICK-IMG0 (rectangle 60 30 "solid"

SICP Section 3.1.1 - Local state in procedures seems inconsistent

╄→гoц情女王★ 提交于 2019-12-14 02:18:54
问题 I am working my way through SICP. I am on Section 3.1.1 and looking at local state. I am evaluating these exercises in GNU Guile v2.0.11. I did find a similar question about this section, but it seems not to address the point I am struggling with, or I am overly obtuse. The two examples I am looking at are these: (define new-withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))) (define (make-withdraw

Deep-reverse for trees in Scheme (Lisp)

五迷三道 提交于 2019-12-14 01:12:08
问题 I have a deep reverse for a basic tree data structure in Scheme (define (deep-reverse t) (cond ((null? t) '()) ((not (pair? t)) t) (else (cons (deep-reverse (cdr t)) (deep-reverse (car t)))))) (define stree (cons (list 1 2) (list 3 4))) 1 ]=> (deep-reverse stree) ;Value: (((() . 4) . 3) (() . 2) . 1) I feel like a cleaner, better result would be: (4 3 (2 1)) Can anyone provide some guidance as to where I'm going wrong in my deep-reverse function? Thank you. 回答1: It's better to split the task

Editing programs “while they are running”? How?

冷暖自知 提交于 2019-12-14 00:17:24
问题 This question is a corollary to: Editing programs “while they are running”? Why? I'm only recently being exposed to the world of Clojure and am fascinated by a few examples I've seen of "live coding". The question linked above discusses the "why." My question is: How is this live coding technique possible? Is it a characteristic of the clojure language which makes it possible? Or is it just a pattern that they applied which could be applied to any language? I've got a background in python and

Scheme: how to produce '(5 . (5))

旧巷老猫 提交于 2019-12-13 20:12:42
问题 I have tried all kinds of combinations of cons and append to produce '(5 . (5)) but I couldn't. Is there any way? 回答1: At the risk of sounding like Bill Clinton, it depends on what you mean by "produce". If you mean "produce a value that prints on the screen as '(5 . (5)) , then you're sort of out of luck, because this value prints as '(5 5) . For a similar example: how do I produce the number 1e-1 ? Well, try typing it in; this is the same as 0.1, and if you type in 1e-1, it's going to print

Symbol’s value as variable is void: false when run element-of-setp

流过昼夜 提交于 2019-12-13 20:09:24
问题 Following SICP's instruction, I rewrite its intersection-set as: (defun intersection-set (set1 set2) (cond ((or (null set1) (null set2)) '()) ((element-of-setp (car set1) set2) (cons (car set1) (intersection-set (cdr set1) set2))) (t (intersection-set (cdr set1) set2)))) (defun element-of-setp(x set) (cond ((null set) false) ((equal x (car set)) t) (t (element-of-setp x (cdr set))))) (intersection-set (list 1 2) (list 2 3 4)) Running it reports the following error: element-of-setp: Symbol’s

Scheme - Replacing elements in a list with its index

青春壹個敷衍的年華 提交于 2019-12-13 19:30:59
问题 I am trying to replace the elements in a scheme list with its position. For example, calling: (position '((a b) c)) should return: '((0 1) 2) So far, my code keeps the list format, but the index is not updating. (define (position term1) (define index 0) (cond [(null? term1) '()] [(list? term1) (cons (position (car term1)) (position(cdr term1)))] [else (+ 1 index) index])) When (position '((a b) c)) is called, it returns '((0 0) 0) Can anybody explain why the index isn't updating? 回答1: There

(SCHEME) Number -> English List

依然范特西╮ 提交于 2019-12-13 19:28:08
问题 Okay. So I'm wondering how to create a function that will turn a random number into its english word component. Such as (1001 -> '(one thousand one) or 0 -> '(zero) and (number-name factorial 20 -> ’(two quintillion four hundred thirty two quadrillion nine hundred two trillion eight billion one hundred seventy six million six hundred forty thousand)) I worked with a previous user on stackoverflow to get something that turned a long number into 3 part digits (1,341,100 is one million, 341

Recursion in scheme

让人想犯罪 __ 提交于 2019-12-13 19:19:23
问题 My goal is to find from this list (A B C D E G) a new list (B D G) But my code is not working. (define (fun lst) (cond ((null? lst) '()) ((null? (cdr lst) '()) (else (cons ( cadr lst) ( fun lst)))) I'm getting (B C D E G) . Where have I gone wrong? 回答1: Your function isn't doing anything and you really haven't specified what you want to test. If you really only want to get B D G from that specific list then all you need to do is make those each tests, (equal? (car lst) 'B) and so on for each