Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Structures in Scheme

I am learning Scheme, coming from a background of Haskell, and I've run into a pretty surprising issue - scheme doesn't seem to have custom data types??? (ie. objects, structs, etc.). I know some implementations have their own custom macros implementing structs, but R6RS itself doesn't seem to provide any such feature.

Given this, I have two questions:

  1. Is this correct? Am I missing a feature that allows creation of custom data types?
  2. If not, how do scheme programmers structure a program?

For example, any function trying to return multiple items of data needs some way of encapsulating the data. Is the best practice to use a hash map?

(define (read-user-input)
    (display "1. Add todo\n2. Delete todo\n3. Modify todo\n")
    (let ((cmd-num (read)))
    (if (equal? cmd-num "1") '(("command-number" . cmd-num) ("todo-text" . (read-todo)))
    (if (equal? cmd-num "2") '(("command-number" . cmd-num) ("todo-id"   . (read-todo-id)))
                             '(("command-number" . cmd-num) ("todo-id"   . (read-todo-id)))))))
like image 381
functorial Avatar asked Dec 24 '22 08:12

functorial


1 Answers

In order to answer your question, I think it might help to give you a slightly bigger-picture comment.

Scheme has often been described as not so much a single language as a family of languages. This is particularly true of R5RS, which is still what many people mean when they say "Scheme."

Nearly every one of the languages in the Scheme family has structures. I'm personally most familiar with Racket, where you can define structures with struct or define-struct.

"But", you might say, "I want to write my program so that it runs in all versions of Scheme." Several very smart people have succeeded in doing this: Dorai Sitaram and Oleg Kiselyov both come to mind. However, my observation about their work is that generally, maintaining compatibility with many versions of scheme without sacrificing performance usually requires a high level of macro expertise and a good deal of Serious Thinking.

It's true that several of the SRFIs describe structure facilities. My own personal advice to you is to pick a Scheme implementation and allow yourself to feel good about using whatever structure facilities it provides. In some ways, this is not unlike Haskell; there are features that are specific to ghc, and generally, I claim that most Haskell programmers are happy to use these features without worrying that they don't work in all versions of Haskell.

like image 198
John Clements Avatar answered Dec 25 '22 22:12

John Clements