8,213 questions
0
votes
3
answers
93
views
Evaluation of expression in operator position of a list
I am trying to learn Lisp, but it seems like I can not quite get behind how the quotes and the evaluation works. Why does the following expression
((car '(car)) '(a b))
not evaluate to a in the REPL? ...
1
vote
2
answers
155
views
Replacing the element in the list with the specific index using `foldr` —— Exercise 1.19 of Essentials of Programming Language
Here is the question from Mitchell Wand's Essentials of Programming Language:
Exercise 1.19 [⋆ ⋆] (list-set lst n x) returns a list like lst, except that the n-th element, using zero-based indexing, ...
2
votes
0
answers
53
views
issues with set-car! when implementing SICP evaluator in Racket
I am implementing circular evaluator in Chapter 4 SICP with racket, it seems that the code running fine only it cannot add new frames to the environment due to set-car! issue.
I tried the followings:
...
1
vote
1
answer
80
views
Scheme: calling `define-syntax-rule` in a loop
I wonder whether it is possible to call define-syntax-rule in a loop-like construction like for-each. I explicitly say 'loop-like' because I know that normal Scheme evaluation comes after macro ...
1
vote
1
answer
113
views
call/cc and multiple values
I'm playing around with implementing looping using call/cc and wrote a map function like this:
(define (map1 f l)
((lambda (state)
(let ((cc (car state))
(l (cadr state))
(...
1
vote
1
answer
63
views
What are "array increments" and "row-major order" in Guile Scheme?
In the documentation for the array_copy function in Guile Scheme is a warning that in the copy, "the array increments may not be the same as those of src". (Where "src" is the ...
4
votes
2
answers
84
views
Graph recursive walk in Scheme
I have an undirected graph which nodes are numbered from 0 to 5. Adjacencies are given with a vector of lists #((1 2) (0 3) (0) (1) (5) (4))), thus node 0 is connected to nodes 1 and 2, node 1 is ...
2
votes
1
answer
129
views
Don't understand Scheme "unbound identifier" message for this function definition [duplicate]
For this code (using Racket IDE) when doing "Run"
#lang scheme
(define (queue! q x)
(let ( (b (list x))
)
(if (null? (car q))
(set-cdr! q b)
(set-cdr! (car q) b)...
1
vote
3
answers
91
views
Scheme stack-like object
Trying to build a very simple stack structure :
(define (make-stack lst)
(lambda message
(case (car message)
((see) (newline) (write lst))
((empty?) (null? lst))
((push) (begin
...
1
vote
0
answers
59
views
How does dynamic-wind work with delimited continuations?
I'm trying to figure out how to implement dynamic-wind in the context of delimited continuations. Any resources or conceptual model?
While I found reference implementations of dynamic-wind for the ...
1
vote
2
answers
94
views
How to get consistent scientific notation with limited precision in Bigloo Scheme?
I'm working with floating-point numbers in Bigloo Scheme, and I encountered a precision issue when performing a simple multiplication:
(* 0.005 1e-9)
;; => 5.0000000000000005e-12
I was expecting ...
-1
votes
1
answer
68
views
Explain random selection of cons-ed weighted nodes
In Scheme (extempore version) I sometimes use expressions similar to the following
to choose alternative defined callback nodes.
(random (cons 0.1 'node1) (cons 0.2 'node2) (cons 0.7 'node3))
This ...
1
vote
3
answers
138
views
Scheme evaluation of list
I wrote a simple intersection function on two lists:
(define (intersection l1 l2)
(if (null? l1) '()
(if (member (car l1) l2)
(cons (car l1) (intersection (cdr l1) l2))
(...
0
votes
1
answer
49
views
How do I port code using `all` to minikanren-with-symbolic-constraints?
I am reading minirop.pdf and it seems to be written in a dialect of MiniKanren that is outdated. I am interested in how I can port this style of MiniKanren to miniKanren-with-symbolic-constraints, ...
2
votes
1
answer
194
views
How to implement Scheme's float, ceil, and round that work on exact rationals in JavaScript?
I just found about these two expressions in Scheme:
(quotient (expt 1000 999) 998001)
(floor (/ (expt 1000 999) 998001))
They return bigInt that looks like this 100200300 ... 999 (with all numbers ...
-1
votes
2
answers
191
views
Idiomatic way to translate Scheme set-car! and set-cdr! into OCaml?
I have a Scheme program that I want to port to OCaml, and it uses lots of set-car! and set-cdr! inside helper functions.
For nested mutable lists, do I need to make their types like x ref list ref ...
1
vote
1
answer
131
views
How to create Clojure-style loop/recur macro using syntax-case in Guile Scheme?
I'm learning syntax-case macro system. This is what I have so far:
(define-syntax loop
(lambda (x)
(syntax-case x ()
[(_ (var val x ...) body)
(with-syntax ([recur (datum->syntax ...
0
votes
1
answer
89
views
Error: submacro used in `define-record-type` field declaration not expanding
I want to create a macro with fields defined as (<field-name> <field-getter> [ setter <field-setter> ] [ default <field-default> ]) (default is for future developments). To ...
0
votes
1
answer
86
views
How to avoid evaluating one statement excluded by predicate during the conditional like `if` in Scheme?
Here is the minimal example:
1 ]=> (if #f (quasiquote ((unquote if))) (quasiquote (if)))
;Classifier may not be used as an expression: #[classifier-item 12]
2 error> (quasiquote (if))
;Value: (...
0
votes
1
answer
74
views
"Splicing" `values` in to a function call
Can Scheme "splice" the result of values or a list as the arguments for a function call? For example (imagined syntax): (list 0 (values 1 2) (values 3 4) 5) would return (0 1 2 3 4 5). The ...
1
vote
1
answer
64
views
GNU guile pattern matching: quasi-quote "ooo" quantifier pattern not work
I encountered some errors with pattern matching.
For quasi-quote, the "ooo" pattern including "..." does not work. I looked at source code "guile-3.0.10/module/ice-9/match....
0
votes
0
answers
57
views
Syntax error with how I am using lambda: expected (x1 x2 ...)
I am doing this question, and others, for HW but when I run my code it is giving me this error:
enter image description here
(define prefix?
(lambda (xs ys)
(if (null? xs)
#t
(if ...
1
vote
1
answer
83
views
what is the proper way to evaluate Lisp/Scheme in emacs
I try this LISP snippet from the Structure and Interpretation book
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
in emacs by C-x C-e, or by evaluating the whole buffer. either way
and get ...
0
votes
1
answer
56
views
Cons and dot product related?
Does a Lisp or Scheme cons bear any similarities or theoretical kinship to a dot product? In the Lisp family (cons 1 2) produces (1 . 2) which looks a lot like a dot product. Just coincidence?
2
votes
1
answer
132
views
How to do the general match for segment variable in unification?
This is one exercise from Software Design for Flexibility (SDF) book:
Here is an example:
;;; A missing match!
(unify:internal '(((?? x) 3) ((?? x)))
'((4 (?? y)) (4 5))
...
0
votes
3
answers
149
views
How to download a file in guile?
I can't seem to find a way or an api to download a file in guile, I'm a beginner so I don't know how to add libraries, I'm using the guix package manager.
I manage to make some code like this.
(use-...
1
vote
1
answer
112
views
call/cc mutable environment storage
I have the following Scheme program:
(define sg-pair (let ()
(define x #f)
(cons
(call/cc (lambda (c)
(set! x c)))
...
1
vote
1
answer
116
views
Scheme macro where I need an index number for each repeated element
I am trying to make a macro similar to register-groups-bind in the Common Lisp cl-ppcre library. The idea is that you make a regex with groups, and give it a list of variables, statements to execute. ...
0
votes
2
answers
144
views
What is the Lisp's symbol equivalent of JavaScript?
So in Lisp/Scheme, there are these 'symbols', which are basically, if I understand correctly, and correct me if I'm wrong, references to variables (not their values). I was wondering if there's a ...
1
vote
1
answer
119
views
In Scheme, is there a way to print a human readable version of a continuation?
I'm trying to make a function in Scheme that asks the user for input to complete a continuation. As part of this, I want to print out the continuation so the user can see what will happen with their ...
1
vote
0
answers
64
views
Is it necessary to use lazy evaluation instead of stream for lazy tree with Scheme language?
This is from one footnote in SICP:
Note that these lazy lists are even lazier than the streams of chapter 3: The car of the list, as well as the cdr, is delayed. ^[41]
[41] This permits us to create ...
0
votes
0
answers
55
views
"Variable reference to a syntactic keyword" when using syntax-rules
minimal working code demo:
(define (demo-tree)
(custom-cons 0 1))
(define-syntax custom-cons
(syntax-rules ()
(
; (_ x y)
(custom-cons x y)
(cons-stream x y)
)))
(custom-...
0
votes
2
answers
64
views
How to check if any of the elements in the list of lists in not null in Scheme?
In my Scheme interpreter written in JavaScript, I want to modify the some function that that checks if any of the elements in the list return true. With implementation that allow to pass multiple ...
1
vote
2
answers
85
views
Why can we use first-class expression in place of first-class function?
This is from this CS 61A notes about SICP p80~82 about ucblogo (a dialect of Logo, which derived from Lisp):
Second, Logo has first-class expressions; you can run a list that you get as an argument.
...
0
votes
0
answers
70
views
pass value of a variable with the same name between procedures/functions in Scheme
I want to pass values of variable with the same name (say "bindings") between procedures, but have problems with it. For example, I have several procedures like the one below, they all have ...
1
vote
0
answers
74
views
Can we modify R7RS implementation of the derived expression type `do` to finish SICP exercise 4.9?
This is one follow-up question of this QA where how syntax-rules works has been known owing to Shawn. As someone told me before, one different question although somewhat related should be posted as ...
0
votes
0
answers
161
views
How to implement one anonymous loop form like do in the evaluator as a derived expression using Scheme?
This is from SICP exercise 4.9.
Exercise 4.9. Many languages support a variety of iteration constructs, such as do, for, while, and until. In Scheme, iterative processes can be expressed in terms of ...
1
vote
1
answer
71
views
Do tail-recursive functions reuse a single environment in Scheme?
I'm currently taking an undergraduate course in functional programming, and we just learned about environments in Scheme. From what I understand, an environment is the context in which a function is ...
3
votes
4
answers
297
views
Can a recursion with nested calls in tail position be tail recursion?
Can a recursion with nested calls in tail position be tail recursion?
For example, I have the following function in Racket that is intended to convert a binary tree, defined as a nested struct below, ...
0
votes
1
answer
104
views
Does GNU Guile not have the vector->string function?
According to scheme.org Implementations GNU Guile supports the R6 and R7 versions of Scheme. And the guile executable has command line arguments of "--r6rs" and "--r7rs". And the ...
1
vote
0
answers
97
views
Draw the call stack for a cc/call in Racket
Everyone says that with call/cc the stack is saved and then restored back when I call the continuation. In literally all answers and questions here, I've never seen a picture of their explanations ...
1
vote
1
answer
82
views
Redefining the special form after the usage of that special form in one func definition has no effects to that definition in Scheme
I am reading SICP section 3.5.1 where it gives the implementation of primitive procedures provided in MIT/GNU Scheme.
I tried to load these implementations without considering the order since they are ...
1
vote
0
answers
90
views
Why do fully-functional map, filter, and fold-right/fold-left need environment model?
This is one follow-up question of this QA. The comments there focus on how to implement map etc in C++ but doesn't say much about "relationship between environment model" and map etc.
SICP ...
0
votes
2
answers
168
views
Is environment model necessary for higher-order procedures?
When learning SICP, 6.001 lec15 has:
A good understanding of the environment model tells me why (IMHO) C++ will never have a fully-functional map, filter, and fold-right/fold-left procedures that are ...
0
votes
0
answers
58
views
What should I do with the unbound variable?
(define (subtractMatHelper list1 acc)
(if(null?list1)
acc
( (define newList (cons(-(car(car list1)) (car(car(cdr list1)))) acc)) ;the subtraction of the first element inthe ...
2
votes
1
answer
208
views
How does recursive tail optimization work?
How does recursive tail call optimization work in regards to what is placed/removed from the call stack? Specifically for LISP like languages? Any insight at assembly level will be helpful.
I show ...
2
votes
3
answers
92
views
How to evaluate conditionals not deterministic (random order) in Racket/Scheme?
How do I evaluate this code un-deterministically?
(cond (correct-1? do-thing-1)
(correct-2? do-thing-2)
(correct-3? do-thing-3))
For example, if correct-1? and correct-3? are true, then I ...
0
votes
1
answer
63
views
Getting wrong answer for cube-root procedure in Scheme [closed]
I wrote this code to compute the cube root of a number in Scheme:
(define (square x) (* x x))
(define (abs x) (if (< x 0) (- x) x))
(define (cube x) (* x x x))
(define (cube-root-itr guess x)
(...
0
votes
1
answer
52
views
Unbound variable error in cube root function
The code is as follows:
(define (square x) (* x x))
(define (abs x) (if (< x 0) (- x) x))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
...
0
votes
0
answers
48
views
Adding one careless dot in Scheme code works with one weird result without throwing syntax errors [duplicate]
Recently when I am coding my homework using Scheme language, I carelessly added one dot. Here is one minimal demo:
(define (test value)
(vector-map
(lambda (elem)
(cond
((number?...