Using sort! function in DrRacket - scheme

I'm dealing with vectors and trying to use sort! function which is listed here. However I get unbound identifier error. At the top of editor, I specified the language as
#lang scheme
Should I load another module to use this function?

The referenced documentation is for MIT Scheme, you're using Racket with the #lang scheme language. Use the sort procedure instead, which returns a new sorted list:
(define lst1 '(3 2 1 0))
(define lst2 (sort lst1 <))
lst1
=> '(3 2 1 0)
lst2
=> '(0 1 2 3)
If you need to modify the input list after sorting it, use:
(define lst1 '(3 2 1 0))
(set! lst1 (sort lst1 <))
lst1
=> '(0 1 2 3)

The documentation you link to is for mit-scheme rather than to Racket.
The documentation for Racket is here: http://docs.racket-lang.org/search/index.html?q=sort
Note that the scheme in #lang scheme doesn't mean R5RS or R6RS, but rather
the "MzScheme language". Nowadays one most people use #lang racket.
#lang scheme
(require rnrs/sorting-6)
(define v (vector 5 3 2))
(vector-sort! < v)
v
If you want to use R6RS with Racket here is an example:
#!r6rs
(import (rnrs lists (6))
(rnrs base (6))
(rnrs io simple (6)))
(display (find even? '(3 1 4 1 5 9)))

Just define your own sort! using a syntactic extension:
(define-syntax-rule (sort! lst p ...)
(set! lst (sort lst p ...)))
then
> (define lst1 '(3 2 1 0))
> (sort! lst1 <)
> lst1
(0 1 2 3)

DrRacket (or just racket in the CLI) is a multiple language implementation. To strangest thing is that #lang scheme (or just #!scheme) isn't following a Scheme standard at all but is the legacy name of a language that was R5RS compatible once, but has changed it's name to #!racket and has immutable pairs as standard.
Other languages supported by the racket program that uses mutable pairs are #!r5rs and #!r6rs. These follow the Scheme standard and if you use these you can compile and run your programs with other implementations as well. By just changing the first line to that you are telling that the rest of this file will be programmed in that language. R6RS has vector-sort while R5RS needs support by SRFI-95 sort library's sort!
PS: If you are following SICP you might be interested in #lang planet neil/sicp. See here if you need help getting it to work.

Related

Does specifying the number of elements on defining a vector in Racket make the program use less time or memory?

For example, can this
#!/usr/bin/env racket
#lang racket/base
(define vector-of-pleasures #6("eating" "cold showers" "swimming" "running" "pullups" "weighlifting"))
(for ((pleasure vector-of-pleasures)) (displayln pleasure))
be more performant because of the optional annotation than this
#!/usr/bin/env racket
#lang racket/base
(define vector-of-pleasures #("eating" "cold showers" "swimming" "running" "pullups" "weighlifting"))
(for ((pleasure vector-of-pleasures)) (displayln pleasure))
?
Methinks it should not matter since in both snippets vector-of-pleasures is immutable.
What are, other than performance, possible reasons for annotating the number of the elements of the vector on its definition?
No, it doesn't. The real use of that syntax is that it lets you write a vector of a bunch of identical things easily: #6(1) is a vector of 6 1s.
But I think it could save space. I can't see why, for instance:
(let ((v #2((1 2))))
(eq? (vector-ref v 0) (vector-ref v 1)))
should not be true. But then this rapidly turns into the question of when similar literals can be folded: can (eq? '(1 2) '(1 2)) return true for instance? I don't know what Racket's rules are on that.

Map-filter CPS version in Scheme

I'm wondering, is there any implementation of map-filter in CPS version in Scheme?
example:
(map-filter square even? '(1 2 3 4)) -> '(4 16)
You can define map-filter& using filter& and map&:
(define (map-filter& proc& pred?& lst k)
(filter& pred?&
lst
(lambda (new-lst)
(map& proc& new-lst k))))
[This answer give two versions of the function the person described: not CPS versions of it. I think the question is ambiguous but this is probably not the right answer: Sylwester's is.]
If what you want is what you've described (which I'm fairly sure is not CPS), then something like this works:
(define (map-filter mapper filterer lst)
(map mapper (filter filterer lst)))
In Racket then this might be better as this:
(define (map-filter mapper filterer lst)
(for/list ([e (in-list lst)]
#:when (filterer e))
(mapper e)))
which perhaps has a better chance of avoiding consing an intermediate list but smells nastier to me.

passing known arguments to Scheme function

I have a function primeFactors that I want to take in a number, and return a list of the factors of the number. It appears that
(define (primeFactors x '()) ...FOOBAR)
is invalid, however the workaround of
(define (primeFactors x) (primeFactors2 x '()))
with primeFactors2 defined elsewhere would work, because I can pass the empty list as an argument when calling the function but not defining it. Is there a less awful way of doing this kind of predetermined argument passing?
R7RS-Small and R6RS has case-lambda in the library (scheme case-lambda) and (rnrs control 6):
#!r7rs
(import (scheme base)
(scheme case-lambda))
(define prime-factors
(case-lambda
((n) (prime-factors n '()))
((n lst) 'implementation)))
For R5RS we have SRFI-89 Positional arguments. Perhaps your favorite implementation has it already and if not you can just fetch it from the spec. Here is how it works:
#!r5rs
(define* (prime-factors x (lst '()))
'implementation)
A pure compatible Scheme way to do it:
;;; (prim-factors n [lst '()]) => lst
(define (prime-factors n . llst)
(define (prime-factors n lst)
'implementation)
(prime-factors n (if (null? llst) '() (car llst))))
The local function is just to make it more efficient in the case you are doing recursions since it's only the initial call that is without the second argument.

Differing behavior between debug and REPL mode in Dr. Racket: modifying a constant

I am trying to code my own unit-test library in scheme. So far, I have the following code in the definition window:
#lang scheme
(define all-tests '())
(define-syntax make-tests
(syntax-rules (->)
[(_ test-name function (args ... -> result) ...)
(begin
(define test-name
(list function (list (list args ...) ...) (list result ...)))
(set! all-tests (cons test-name all-tests)))]))
Using Dr. Racket, when I submit the following code into the REPL:
> (make-tests tests + (1 2 -> 3))
set!: assignment disallowed;
cannot modify a constant
constant: all-tests
However when I tried to debug it in Dr. Racket:
(debug)> (make-tests tests + (1 2 -> 3))
(debug)> tests
(#<procedure:+> ((1 2)) (3))
(debug)> all-tests
((#<procedure:+> ((1 2)) (3)))
So, for some reason, in Dr. Racket v6.1, my code works in debug mode, but does not work when using the REPL. What is going on, and how can I debug my code?
Based on the comment on this previous answer: If a module doesn't mutate a binding you cannot mutate it from outside the module. Running code in the REPL is not the same as running code in the module. A racket source in Racket language is a module and #lang scheme is not Scheme but synonym for #lang racket.
So to get the behaviour you would like you can replace (define all-tests '()) with:
(define all-tests #f)
(set! all-tests '()) ; quick fix that makes all-test mutable
And it will work. You may dig deeper into racket documentation about it too.
You can Un-check the option Enforce constant definition In the choose language menu in DrRacket In the Dynamic properties box.
And as a note to your macro. Why do you specify a name for the test? The name is not placed in the list, and aside from defining it to cons it to all-tests it is never used. You can use a let instead. Unless of course your are going to do something with tests later.
(define-syntax make-tests
(syntax-rules (->)
[(_ function (args ... -> result) ...)
(let
((test (list function (list (list args ...) ...) (list result ...))))
(set! all-tests (cons test all-tests)))]))

How can I unsplice a list of expression into code?

I have an experiment for my project, basically, I need to embedded some s-expression into the code and make it run, like this,
(define (test lst)
(define num 1)
(define l (list))
`#lst) ; oh, this is not the right way to go.
(define lst
`( (define num2 (add1 num))
(displayln num2)))
I want the test function be like after test(lst) in racket code:
(define (test lst)
(define num 1)
(define l (list))
(define num2 (add1 num)
(displayln num2))
How can I do this in racket?
Update
The reason I would like to use eval or the previous questions is that I am using Z3 racket binding, I need to generate formulas (which uses racket binding APIs), and then I will fire the query at some point, that's when I need to evaluate those code.
I have not figured out other ways to go in my case...
One super simple example is, imagine
(let ([arr (array-alloc 10)])
(array-set! arr 3 4))
I have some model to analyze the constructs (so I am not using racketZ3 directly), during each analyzing point, I will map the data types in the program into the Z3 types, and made some assertions,
I will generate something like:
At allocation site, I will need to make the following formula:
(smt:declare-fun arr_z3 () IntList)
(define len (make-length 10))
Then at the array set site, I will have the following assertions and to check whether the 3 is less then the length
(smt:assert (</s 3 (len arr_z3)))
(smt:check-sat)
Then finally, I will gather the formulas generated as above, and wrap them in the form which is able to fire Z3 binding to run the following gathered information as code:
(smt:with-context
(smt:new-context)
(define len (make-length 10))
(smt:assert (</s 3 (len arr_z3)))
(smt:check-sat))
This is the super simple example I can think of... making sense?
side note. Z3 Racket binding will crash for some reason on version 5.3.1, but it mostly can work on version 5.2.1
Honestly, I don’t understand what exactly you would like to achieve. To quote N. Holm, Sketchy Scheme, 4.5th edition, p. 108: »The major purpose of quasiquotation is the construction of fixed list structures that contain only a few variable parts«. I don’t think that quasiquotation would be used in a context like you are aiming at.
For a typical context of quasiquotation consider the following example:
(define (square x)
(* x x))
(define sentence
'(The square of))
(define (quasiquotes-unquotes-splicing x)
`(,#sentence ,x is ,(square x)))
(quasiquotes-unquotes-splicing 2)
===> (The square of 2 is 4)
Warning: if you're not familiar with how functions work in Scheme, ignore the answer! Macros are an advanced technique, and you need to understand functions first.
It sounds like you're asking about macros. Here's some code that defines test to be a function that prints 2:
(define-syntax-rule (show-one-more-than num)
(begin
(define num2 (add1 num))
(displayln num2)))
(define (test)
(define num1 1)
(show-one-more-than num1))
Now, I could (and should!) have written show-one-more-than as a function instead of a macro (the code will still work if you change define-syntax-rule to define), but macros do in fact operate by producing code at their call sites. So the above code expands to:
(define (test)
(define num1 1)
(begin
(define num2 (add1 num1))
(displayln num2)))
Without knowing the problem better, it's hard to say what the correct approach to this problem is. A brute force approach, such as the following:
#lang racket
(define (make-test-body lst)
(define source `(define (test)
(define num 1)
(define l (list))
,#lst))
source)
(define lst
`((define num2 (add1 num))
(displayln num2)))
(define test-source
(make-test-body lst))
(define test
(parameterize ([current-namespace (make-base-namespace)])
(eval `(let ()
,test-source
test))))
(test)
may be what you want, but probably not.

Resources