scheme

Problem with list in Lisp

本小妞迷上赌 提交于 2020-01-04 06:04:43
问题 I am trying to write a simple procedure in Lisp to insert an element into binary search tree. I represented the tree as a list: the first element in the tree is the root the second element is the left sub-tree the third element is the right sub-tree This is my code: (define Insert (lambda (x T) (if (null? T) (list x '() '()) (if (> x (car T)) (list (car T) (cadr T) (Insert x (list (caddr T)))) (list (car T) (Insert x (cadr T)) (list (caddr T))))))) When I call the procedure like this: (Insert

Scheme/Racket filter/map multiple arguments

a 夏天 提交于 2020-01-04 05:08:31
问题 Lets say I want to do the following: (define (foo lst x) (filter function lst) but function takes in 2 arguments (and function was given to me), one being the list lst it will use, and the other being x . Syntactically, how would I change that line to pass in the second argument? Sorry I am new to Scheme/DrRacket. 回答1: Try this, using curry: (define (foo lst x) (filter (curry function x) lst)) That is, assuming that function takes as first parameter x and as second parameter each one of the

What is fixed point?

孤街浪徒 提交于 2020-01-04 04:12:24
问题 I'm rewatching some of the earlier lectures on SICP. The notion of a fixed-point is a bit confusing to me. The fixed-point procedure: should I be thinking about it this way, "it's the way to find a fixed-point of a given function." So given f(2) = 2 ? Also why is it stated in this lecture, that a new function y that maps to x / y is a fixed point? 回答1: According to Fixed point (mathematics) on Wikipedia: In mathematics, a fixed point (sometimes shortened to fixpoint, also known as an

Efficient incremental hash computation during program interpretation

那年仲夏 提交于 2020-01-04 04:05:08
问题 I'd like to write a recursively memoizing Scheme interpreter . At any point during evaluation, the interpreter should be able to detect when it receives as arguments a pair of expression and environment that it has previously seen. Plain memoization of eval and apply is inefficient . It would require looking up the arguments in a hash table on every call of eval / apply , which would require walking the entire (possibly big) arguments on hash table matches. For example, assume that the

Namespaces in Chicken Scheme

寵の児 提交于 2020-01-04 02:42:09
问题 How do namespaces work in Chicken Scheme? I am now using the parley egg, and when I define a function with the name e.g. read , that causes an error because of name clashing (actually, because my read overwrites parley 's own read , and it is invoked with wrong type. Here's the code: (use parley) (define (read p) p) ; This `read` function conflicts. (let loop ((l (parley "> "))) (if (or (eof-object? l) (equal? l "quit")) (print "bye!") (begin (printf "you typed: ~s~%" l) (loop (parley "> ")))

Using scheme/racket to return specific items from a list

[亡魂溺海] 提交于 2020-01-04 02:30:48
问题 What I would like to do is create a function that takes in a list of values and a list of characters and coalesce the corresponding characters("atoms" I think they would technically be called) into a new list. Here is what I have so far; #lang racket (define (find num char) (if (= num 1) (car char) ;Problem here perhaps? (find (- num 1) (cdr char)))) (define (test num char) (if (null? num) '("Done") (list (find (car num) (test (cdr num) char))))) This however gives me an error, which for the

Append! in Scheme?

☆樱花仙子☆ 提交于 2020-01-03 18:59:32
问题 I'm learning R5RS Scheme at the moment (from PocketScheme) and I find that I could use a function that is built into some variants of Scheme but not all: Append! In other words - destructively changing a list. I am not so much interested in the actual code as an answer as much as understanding the process by which one could pass a list as a function (or a vector or string) and then mutate it. example: (define (append! lst var) (cons (lst var)) ) When I use the approach as above, I have to do

How to find partitions of a list in Scheme

痞子三分冷 提交于 2020-01-03 17:19:08
问题 Say there is any given list in Scheme. This list is ‘(2 3 4) I want to find all possible partitions of this list. This means a partition where a list is separated into two subsets such that every element of the list must be in one or the other subsets but not both, and no element can be left out of a split. So, given the list ‘(2 3 4) , I want to find all such possible partitions. These partitions would be the following: {2, 3} and {4}, {2, 4} and {3}, and the final possible partition being

The mechanism of anonymous function to call itself in Scheme?

不羁的心 提交于 2020-01-03 14:07:26
问题 I'm reading The Little Schemer and feel confused about the following code: ((lambda (len) (lambda (l) (cond ((null? l) 0) (else (+ 1 (len (cdr l))))))) eternity) (define eternity (lambda (x) (eternity x))) The code is to determine the empty list, otherwise it never stops. Why is the " len " not recursion? 回答1: Although it can be dangerous to apply textual substitution to Lisp forms (since there are dangers of multiple evaluation, etc.), in this case it may help to look at this form and see

The mechanism of anonymous function to call itself in Scheme?

南楼画角 提交于 2020-01-03 14:07:09
问题 I'm reading The Little Schemer and feel confused about the following code: ((lambda (len) (lambda (l) (cond ((null? l) 0) (else (+ 1 (len (cdr l))))))) eternity) (define eternity (lambda (x) (eternity x))) The code is to determine the empty list, otherwise it never stops. Why is the " len " not recursion? 回答1: Although it can be dangerous to apply textual substitution to Lisp forms (since there are dangers of multiple evaluation, etc.), in this case it may help to look at this form and see