1

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?

Community
  • 1
  • 1
mtyurt
  • 3,369
  • 6
  • 38
  • 57

4 Answers4

3

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)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

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)))
soegaard
  • 30,661
  • 4
  • 57
  • 106
1

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)
uselpa
  • 18,732
  • 2
  • 34
  • 52
0

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.

Community
  • 1
  • 1
Sylwester
  • 47,942
  • 4
  • 47
  • 79