This question already has an answer here: How do I pass a list as a list of arguments in racket? 2 answers I have a function which takes an unlimited number of args such as (define (func . args)). Say I have a list '(1 2 3), how do...

Say I have this code: #lang racket (define a ((λ (x) x) ((λ (y) y) (λ (z) ((λ (w) w) z))))) I know intuitively that this lambda expression is (extensionally) equal to (λ (z) z) My question is if there is a way to print out the body of a...

Could someone please explain what happens once the continuation is called for this. ((cdr (or (call/cc (lambda (cc) (cons 2 (lambda () (cc #f))))) (cons 3 5)))) ((cdr (or (call/cc (lambda (cc) (cons 2 (lambda () (cc #f))))) (cons 3 (lambda() (+ 3 2)))))) The first statement gives error but...

I've tried looking at the docs but i can't find an example that i can use for my case. I need to import break from srfi/1. Here's my attempt. The example works in #lang racket. #lang typed/racket (require/typed (only-in srfi/1 break) ;[break (All (T) (-> (-> T Boolean) (Listof T)...

My baseline for this questions comes from MIT's Structure and Interpretation of Computer Programs. In the book, a filter function is defined. I know that map is part of the spec, but I see nothing resembling filter. EDIT: Specifically I'm referring to the spec here: http://www.schemers.org/Documents/Standards/R5RS/HTML/...

The following procedure, written in Scheme, (define (eat xs) (lambda (x) (if (eq? x 'vomit) (reverse xs) (eat (cons x xs))))) can be called in the following way: > eat #{procedure 9165 eat} > (eat '()) #{procedure 9166 (unnamed in eat)} > ((eat '()) 1) #{procedure 9166 (unnamed in eat)}...

I need to convert strings to 26-ary and then be able to convert them back. My current code is: (define (26-ary-word s) (let ([len (string-length s)]) (let f ([n 0] [acc (+ (- (char->integer (string-ref s 0)) 97) 1)]) ; adding 1 so that all strings start with 'b' (if...

I'm trying to decipher the documentation call-with-continuation-prompt Applies proc to the given args 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....

Consider the following example: #lang racket (match '(cat . doge) [`(,a . ,b) (match b [a #t] [_ #f])] [_ "Not a pair"]) This is what I might write if I wanted to match pairs where the head and tail are the same. This doesn't work though because the second...

I am looking for a built-in function in Racket that will return True iff all the items in a list are true. I tried: (define (all lst) (when (equal? lst '()) #t) (if (not (car lst)) #f (all (cdr lst)))) Giving error: car: contract violation expected: pair? given: '() A...

I need catch the numbers of function numerosR by separately because them I need know the major number, minor number and average of all numbers, but I don't know this is my code: (clear) (display "Ingrese un Numero entre 10 y 40: ") (define Numero (read)) ; Funcion numerosRamdon genera...

I am writing a program to check if a number is prime in Scheme. However, I have written the following code. I check out another question on StackOverflow where the OP was getting the same error. I checked whether I was committing the same mistake. I was not. I have...

I am writing a program in Scheme to manipulate some lists and some self defined types. I have written the following short code and it gives me the following error: Source expression failed to match any pattern Why does this error crop up? I think this is because I used...

Hi guys i'm wanted to know if i have the correct expression for this picture, if not why please (a((f(b c))(g h))e) ...

(define rearrange (λ ignore (define (proc1 x y) (+ x y)) (foldr (λ (x y) (if (list? x) (append (rearrange x y) y) (if (procedure? x) (append y (list x)) (cons x y)))) empty '(a proc1 b)))) why isn't x being recognized as a procedure even though I defined it...

I wonder how to simulate the scheme define in common lisp,and want to write a macro for simulate the define. So what's the different between cl's defun deparameter defvar and scheme's define ,and how i can do this?

I am trying to write a Racket function that takes in a list of numbers as the input, and outputs the sum of the square roots of those numbers in the list that are perfect squares. The code I have currently does not compile, and is as follows: (define (sum-of-perfect-roots...

This question already has an answer here: Use of lambda for cons/car/cdr definition in SICP 2 answers I can't seem to explain how this definition of cons, car and cdr works: (define (p-cons x y) (lambda (proc) (proc x y))) (define (p-car proc) (proc (lambda (p q) p))) (define...

I have a list in Scheme which contains numbers. I wrote a function that takes as input one number and a list of objects and checks if the confidence value of the object matches the number. Once I get the object whose confidence values matches the given number, I want...

I'm a newbie and I didn't understand very well the language. Could anyone please explain to me what this functions do? First function: (define (x l) (cond ((null? l) 0) ((list? (car l)) (+ (x (car l)) (x (cdr l)))) (else (+ 1 (x (cdr l)))) )) Second function: (define...

I'm seeing an issue when swapping the order of parameters to the - function. ; source (define (compose f g) (lambda (x) (f (g x)))) (define (repeated f n) (if (= n 1) f (compose f (repeated f (- 1 n))) ; causes an out of memory error (compose f...

Okay, so I am trying to turn a list of a list of numbers into an image where each number represents a block with a color assigned to that number. For example: (define allig '((1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

Is it possible to update the program code while it is being interpreted by csi, the Chicken Scheme Interpreter? If so, how? So that I can interactively change part of the code and immediately see the effects of that changes. For example, suppose I have written the following program: (define...

I have 2 dynamic lists that I would like to merge into one. Say '(1 2 3 4) and '(15 16) and get '(1 2 3 4 15 16) How can this be done?...

I am a newbie in scheme, and I am in the process of writing a function that checks pairwise disjointess of rules (for the time being is incomplete), I used symbols and lists in order to represent the rues of the grammar. Uppercase symbol is a non-terminal in the grammar,...

I seem to have a case of disappearing items after invoking sort! on a list of strings: (I have derived this code from Autogen template that I was working on.) (use-modules (ice-9 pretty-print)) (let ((files '("log4cplus/socketappender.h" "log4cplus/qt4debugappender.h" "log4cplus/nteventlogappender.h" "log4cplus/ndc.h" "log4cplus/msttsappender.h" "log4cplus/mdc.h" "log4cplus/loglevel.h" "log4cplus/loggingmacros.h" "log4cplus/logger.h" "log4cplus/log4judpappender.h" "log4cplus/log4cplus.h" "log4cplus/layout.h" "log4cplus/hierarchylocker.h"...

My recursive function correctly sums all the integers, inclusively, between a and b. However, when I swap my arguments, the result is incorrect. I am reading SICP. My code is (scheme): (define (sum-integers a b) (if (> a b) (if (= (- a 1) b) (+ a b) (+ a(sum-integers...

Example: (split '(1 2 3 4) '3) the Answer should be: ((1 2 3) 4) The function required 1 list and 1 number, the output should be nested list the nested list consist of all elements of "mylist" which are equal or less than the "num", and the greater number...

So I'm fiddling with Racket. Recursively generating n-grams from a list of words was pretty straightforward: (define (n-grams-recursive words n) (if (< (length words) n) '() (cons (take words n) (n-grams-recursive (cdr words) n)))) And the iterative version also pretty straightforward: (define (n-grams words n) (define (iter n-grams remaining-words) (if...

Both CL and Scheme define (and) to return t (or #t) with no arguments. I'm trying to understand the rationale for this. My naive assumption is that an empty set is false, and passing in zero arguments feels like passing in nothing that can be true. Edit: clojure follows the...

I have a function in scheme (extract-disjunct word nodes) that takes two parameters: word and nodes. I have a sentence: "This is a test sentence". For each word in this sentence I want to apply the above given function. This can be done with a simple map operation. However, the...

Having a little trouble understanding the core terms of the MicroKanren DSL. Section 4 says: Terms of the language are dened by the unify operator. Here, terms of the language consist of variables, objects deemed identical under eqv?, and pairs of the foregoing. But they never describe what the "pairs"...

I am basically new to Scheme, and this is my second attempt to solve a certain problem. So what I am trying to do basically is sum each parameter we pass into the function, with an appropriate value for example: (sum 3 1 2 0 2) ;would return 228 Here...

ive been given a task in Scheme (Dr Racket) to reverse to order of a given digit. The solution should be recursive, and this is what i got this far.. The truth is, im not quite sure if the given algorithm even works because i get: " application: not a...

I'm studying SICP right now. And I found the definition of closure in SICP is (maybe) different from closure definition in other languages. Here's what SICP says: The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to...

I'm learning Racket just for fun. I've got a question. Is there a way to reverse a sequence? A sort of generic function for reversing things like: (sequence-reverse "Hello") (sequence-reverse '(1 2 3)) ...

I have downloaded the MIT/GNU Scheme implementation of LISP in order to understand Structure and Interpretation of Computer Programs. However, I am a bit confused about what I am dealing with when I launch the program. When the program is launched, I get two windows: the "MIT/GNU Scheme" window, and...

I'm doing some exercises in Racket, and ran into a problem I couldn't seem to query the docs for. I want to generate the following curries of modulo for a list of divisors: (define multlist '[3 5]) (define modfuncs (map (lambda x ;@ make some modulos (curry modulo x)) multlist))...

I was wondering if anyone could walk me through tail recursion. I have this procedure I made in Racket and I would like a simple explanation on what steps I should take to utilize what I have in the form of tail recursion. The code in Racket is as follows,...

First of all this might be really basic thing but I do not know how to proceed. I have Guile 2.0.9 and Libctl 3.2.2 installed on my Ubuntu 14.04.1 64-bit LTS. My aim is to write a source file in Scheme then have it interpret by Guile so I do...

How do I use apply in Scheme to multiply the first element of each tuple by a number? Example, if my list x = ( (1 2) (3 4) ) I want to do something like: (apply * 2 (car x)) so that it would return ( (2 2) (6...

I wanted to write a small prototype for something, and one of my first thoughts was, that a multimethod-based implementation would be really cool. While I have used GOOPS in the past with guile, I thought I would give racket a try this time. And googling quickly showed that a...

I have a function which return a list of values. Some of these values may be empty lists themselves, while some are not. However, at the end of every list, there is a #<unspecified> value present. I understand that this value is returned when the function does not return anything....

I'm trying to define a function in Scheme that determines if a five-element list contains a full-house (that is, 3 elements are the same and the other 2 are the same, respectively). I have the outline in my head, although I'm messing up the syntax. I'm using and, let, to...

I'm writing a program in which i have to reuse code from one of my professors. My program is written in Racket and the code i want to reuse is written in r6rs. When I want to test my program it always fails. This is because I call a procedure...

I'm new to Lisp and need help understanding this function and the evaluation of (map length '((a b c) (1 2 3 4 5) (v1 v2 v3 v4 v5 v6)))) The value is (3 5 6) (define (map f list) ; applies function f to each element of list (if...

The way I understand a scheme if-statement is that the first condition is when the if-statement is true, and the second statement is when it is false. What if I want several conditions for when the statement proves true? An example: (if (= a b) (set! ([a 2])) // This...

Or rather, why does (= 1e16 (- 1e16 1)) return true? How can I receive more accurate answer?

I am trying to implement a while loop using recursion with lambda, but I just don't understand how to do it. I am supposed to start with this lambda expression: ((lambda (x) (x x)) (lambda (x) (x x)) My first question is why does this cause 'eternal' recursion? I try...

I'm a bit confused on how to append a list that I've gotten from the assoc procedure into another list, here is what I have: (define new-list (list 'test)) (define source-a (list '(a foo) '(b bar) '(c hello))) (append new-list (assoc 'a source-a)) (display new-list) The output is just (test)...

I am trying to use srfi-1 in guile. I used the following code to include the srfi: (use-modules (srfi srfi-1)) However, I get an error saying that srfi is probably undefined. How should I used srfi? I tried googling this problem, but it seems that I am the first person...

After studying most of The Little Schemer, I've been trying my hand at some recursive solutions to Coderbyte challenges. After some fiddling I threw in cons and thought my upperConsIt would work to look through an array, find all of the instances of a particular letter and capitalize each. Ultimately,...

We are trying to construct a log file using IronScheme, and we have written a code for it using racket. It works fine in racket, but IronScheme throws an error. This is what we have so far: (define write-to-log (lambda(whatToWrite) (with-output-to-file "robot-log.txt" (lambda () (printf (string-append whatToWrite "\r\n" ))) #:exists...

I'm having trouble with the random function. I have a function set up where: (define random-abcd (cond [(equal? (random 4) 0) 'A] [(equal? (random 4) 1) 'B] [(equal? (random 4) 2) 'C] [else 'D] )) However when I run this 'D comes out substantially more often than any of the...

I want the following code to work: (define fn.str "(lambda (x) (displayln x)") (define fn.callable (string->lambda fn.str)) ; and then 2 next lines should be valid (fn.callable 123) (apply fn.callable '(321)) But best I can get is to eval string and get output, instead my target is to get the...

I have a sentence with me, for example, The game is played on a level playing field. Now, I have with me a list of words (played is the). These are random words given to me. Now, I have to order them according to the order they occur in the...

Is there any difference between (define make-point cons) and (define (make-point x y) (cons x y)) ? Is one more efficient than the other, or are they totally equivalent?...

I've somehow been able to draw a small basic image in racket (a house). Now, what I'm stuck with is, using big-bang to draw the image -- and then being able to click the roof of the house and have it change to another color. This is what I have...

I'm using Chicken Scheme 4.9.0.1 on a Cloud9 hosted workspace, built from source. I was trying it out with this (I mostly code with python, so I apologize for the weird parens syntax): (define load-module (lambda (filepath) (begin (load filepath) ) ) ) (define print (lambda (command) (begin (display command)...

Define a function that takes a non-empty list and returns an element of the list selected at random and with equal probability. (Do not use the built-in list-ref procedure.) I'm stuck on this. I feel like you would need to count the number of times the function has run...

There are quite a few questions about the "yin-yang puzzle" already in Stackoverflow: How does the yin yang puzzle work? (I assume this is the first one) Haskell (this is my question) C# 5.0 Typed languages ... (there could be something I didn't find yet) I was wondering when and...

I have 2 lists: (Define list1 '("xx1" "xx2" xx3" "xx4" "xx5")) (Define list2 '("xx2" "xx4" "xx5")) the items in the list above are just an example, but either way it will be a string item. What I need to do is compare both lists and remove the items in list1...

I am new to Scheme, and using gosh running on emacs buffer, in MinGW on Windows. It seems that people use readline or rlwrap to use history and more, but I still cannot figure out to accomplish the setting. Though it may be easy to run one on Linux using...

I'm brand new to Scheme, and this is a homework question, so please no outright answers. Here is my question: Write a recursive procedure (any? arg1 arg2 ...), where the argi's are boolean values. any? returns #t if any of its arguments is #t, and #f otherwise. These are the...

I can't seem to figure how to write a correct uscheme (A derivative of MIT Scheme) function that will return boolean whether a list contains a smaller list. I wrote this. (define sublist? (xs ys) (if (= xs '()) #t (if (= ys '()) #f (if (= (car xs) (car...

I'm trying to create a function that finds the area of overlap between two lists and returns the result of joining the two lists together. The overlap area is the longest suffix of the first list that matches a prefix of the second list and it only appears once in...

I'm trying to port yield and yield from from Python to Scheme. Here is an implementation I've done: (define (coroutine routine) (let ((current routine) (status 'new)) (lambda* (#:optional value) (let ((continuation-and-value (call/cc (lambda (return) (let ((returner (lambda (value) (call/cc (lambda (next) (return (cons next value))))))) (if (equal? status 'new) (begin...

The Typed Racket reference indicates that it's possible to use with-type to created “typed regions” within untyped code. The with-type form allows for localized Typed Racket regions in otherwise untyped code. It’s a little unclear how to actually use this, though. Obviously, using such a feature needs to take place...

I want to create an overloaded Scheme macro for a simple form of polymorphism. That is, a macro smart enough to expand differently when given params of different types, so that (look-up key container) does the "right" thing for different kinds of containers. (define-macro (look-up key container) (cond ((table? container)...

How could one go about flattening a list without using the flatten function built in to racket? I understand that the default implementation of flatten is (define (flatten lst) (cond ((null? list) empty) ((list? (car lst)) (append (flatten (car lst)) (flatten (cdr lst)))) (else (cons (car lst) (flatten (cdr lst))))))...

I need to remove consecutive duplicates from a list in Scheme for ex: (remove '(e f f g h h e e)) should return (e f g h e) This is what I have but I keep getting an error: (define (remove lst) (cond ((null? lst '()) ((null? (cdr lst))...

I need help defining a Scheme function that takes a partition (list of lists) and returns a list of pairs that represents an equivalence relation. I've started writing some code but I'm realizing I'm going in the wrong direction. I've been looking at this for hours with no progress. (define...

Input: '(("may 001" 75 72) ("may 002" 75 75) ("may 003" 70 73) ("june 101" 55 55) ("june 104" 55 54) ("aug 201" 220 220)) Desired output: '(("may 001" 75 72) ("may 002" 75 75) ("may 003" 70 73)) How do I achieve this? I only want the may terms....

In order to demonstrate the effectiveness of tail recursion, I would like a way to access the depth of the call stack dynamically in Scheme. Is there a way to do this? If not, is there a way to do this in other major functional languages (OCaml, Haskell, etc.)?...

Hi i want my function to sum digits such that the result is less than 10 (77 = 14=5). dont know why it's passing the base case (define (reduct n) (if(< n 10) n (+(modulo n 10)(reduct (truncate(/ n 10)))))) ...

I am in the process of designing a small MUD game using racket. In my game, gems which are collected by the player are used to bribe guards. Currently if the player possesses more than 1 gem, the game will not let them bribe a guard. Here is the relevant...

I have a list of objects in Scheme. Each object in turn is a list itself. I am using the map function to iterate over all the elements of the list and apply a function to them. However, I want to apply that function only if the element of the...

I am writing a scheme program for performing operations on polynomials. I am currently working on a method that combines all like terms. For example, if I have the polynomial: 1x^2 + 2x^2 + 4x^3, the function should combine the likes terms 1x^2 and 2x^2 and output the final polynomial...

Here is something you can do in Scheme: > (define (sum lst acc) (if (null? lst) acc (sum (cdr lst) (+ acc (car lst))))) > (define sum-original sum) > (define (sum-debug lst acc) (print lst) (print acc) (sum-original lst acc)) > (sum '(1 2 3) 0) 6 > (set! sum...

A 4-year-old old post suggests that one might be able access the current-seconds and related functions in the r5rs language. Here's why I ask: I'm a high school teacher new to Racket and we are using the r5rs language. I would like to introduce students to functions by starting with...

I want to write a function which for n arguments will create n lists and each contains n-th element for every argument, for example: (aux '(1 2) '(3 4)) = `((1 3) (2 4)) I wrote such a function: (define (aux . args) (if (null? args) '() (cons (map car...

I need to write a recursive function in Scheme which takes a list of atoms and reverses it in linear time. I am only allowed to use define, lambda, cons, car, cdr, cond, let, and null? . Here is what I have so far: (define reverse (lambda (lat) (cond ((null?...

All I'm trying to do is use a when statement to return a value :( I want the functionality of: if(x) return y And I'm trying to use: (when (x) y) But the when statement is not evaluating in a way that exits the function and return y. It just...

I am currently confused with the idea behind functional programming in general. I currently have a working solution to my problem (That is, finding the min and max of a list, and returning these in a new list) but to do that, my solution essentially requires 3 functions, and this...

I am trying to programmatically increment a purely alphabetical string in Scheme. Like this "MA", then "MB" and when it reaches "MZ", it should become "MAA" and so on till "MZZ" and then it should become "MAAA" and so on.The "M" needs to be added as a prefix for the...

I am a beginner in Scheme. I found this question in MIT exam 1 for SICP lecture. What's the value and type for – ((lambda (a) (lambda (b) (+ (sqrt a) (sqrt b)))) 5) I am having a hard time understanding how this function works. I am really confused about...

I am trying to intersect two lists on Racket, but the following code is not working: (define (intersection a b) (if (null? a) '() (if (contains (car a) b) (cond (car a)(intersection (cdr a) b)) (intersection (cdr a) b)))) The result always end up being the first item of list...

I am new to Racket, and I am trying to write a recursive function that takes a number n and returns the sum of the squares of the first n integers. For example, (this-function 3) returns 14 because 14 is 9 + 4 + 1 + 0. I tried creating...

I recently discovered a strange behaviour of racket: Whenever I try to filter a list of directories created via directory-list my REPL returns me an empty list, but when I try the same with an quasiquoted list my REPL returns a correctly filtered list. My questions is now: Why it's...

I need to get the max and min of a list in scheme. I have the whole code but there is something wrong. I am not sure what, maybe with the parenthesis. Can anyone help? #lang racket (define (maxmin L) (cond ((null? L) '()) ((null? (cdr L)) (list (car L)...

Especially why we are using tailr and headr? I am not able to understand. (define (foo lst) (cond ((not (list? lst)) lst) ((null? lst) lst) (else (let* ((tail (cdr lst)) (head (car lst)) (tailr (foo tail)) (headr (foo head))) (append tailr (list headr)))))) ...

Define a procedure likenate that takes a list and returns the list with the symbol 'like inserted between each pair of adjacent elements in the given list. I feel like this should be really easy, but I keep getting lists of lists so the check-expects aren't working. I've been...

I am attempting to learn OpenCL and I installed the OpenCL FFI using raco pkg install opencl, but I am now getting a strange error when I try to use the package: clGetPlatformIDs: Undefined error: -1001 context...: /usr/share/racket/collects/racket/contract/private/arrow-val-first.rkt:255:18 /home/lb/Dropbox/workspace/misc/opencl-test.rkt: [running body] Here is the code snippet: #lang racket (require opencl/c)...

I am failing to understand the ribbon01 code, confused with the brackets. Can somebody explain it to me I am new to scheme (define ribbon01 (lambda (lat) ((letrec ((di (lambda (lat) (display lat)))) di) (cdr lat)))) (define ribbon02 (lambda (lat) (letrec (( di (lambda (lat) (display lat)))) (di (cdr lat)))))...

I'm a new schemer. I just want to ask if I can include if-statements in a lambda? For example, (lambda (x) (if e1 e2 e3)). I don't see why not, but my program just keep failing if I write this way. Thanks a lot! Here's my code here.I'm trying to...

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) '()]...

I would like to do something like this (cons '(someword,string->symbol somevarname) (restoflist)) but somevarname is never replaced by its value. I thought this was possible?...

In order to implement derivative of given polynomial, I need to factorize the polynomial, i.e., (* 3 x y) should be the product of 3 and (* x y). So I implemented a function multiplicand to get the second factor of a product: (define (multiplicand p) (let ((second-factor (cdr (cdr...

I create a function which create list. I want to use that list in an another function so how can I do this? (define (myfunc L n) (if (= n 0) empty (cons (list-ref L (random 26)) (myfunc L (- n 1))))) I want to assing this function as a...

I'm porting a Python script to Racket as a learning experience, and I have this function: (define (check-status) (define git [find-executable-path "git"]) (define-values (ckot out in err) (subprocess #f #f #f git "checkout" "-q" "master")) (define-values (local lout lin lerr) (subprocess #f #f #f git "rev-parse" "@")) (define-values (remote rout...