2

I'm new to scheme. I'm wondering how to sort strings in lexicographical order in scheme. For example:

(sort (list "cat" "apple" "dog"))
(apple cat dog)

In C++, I can have 'A'<'B', but it seems not work in scheme. I have referred online, but most are implemented in popular language, there are very few in scheme. So Can someone provide with an actual code in scheme and explain it? Thank you

zach chen
  • 119
  • 1
  • 10

1 Answers1

1

An idiomatic answer to this question is going to depend a lot on which scheme implementation you're using. So, for instance, in Racket I would write

(sort (list "cat" "apple" "dog") string<?)

I see that you've tagged this question r5rs, and perhaps you're asking whether you can write this in R5RS scheme. Yes, you definitely can. But it's probably simpler just to use whatever your scheme implementation provides.

John Clements
  • 16,895
  • 3
  • 37
  • 52
  • Hi john, thanks for your answer. Yes I'm using r5rs scheme. Now I'm wondering how would you implement the procedure sort. I used car or cdr to operate this. But I found it's hard to skip a position by barely using this two operation. For example (list "apple" "good" "cat" "dog"). On first iterative call. "apple" is added into the result list. then good is added behind "apple". But at this step, if I pass (cdr result-list) to the next call. apple will be missed. So how to move to the next element but also keep the original things. Thank you :) – zach chen Oct 14 '16 at 16:42
  • Sounds like you're looking for section 11.3 of How to Design Programs: – John Clements Oct 14 '16 at 21:01
  • url: http://www.ccs.neu.edu/home/matthias/HtDP2e/part_two.html#%28part._sec~3asort.I%29 – John Clements Oct 14 '16 at 21:02