Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is define-struct in Racket and why are there no variables?

In one of my CS courses at university we have to work with Racket. Most of my programming time before university I spent with PHP and Java and also JavaScript. I know Racket is a functional programming language, just like JavaScript (Edit: Of course it isn't. But I felt like I was doing 'functional' programming with it, which after seeing the answers, is a wrong perception.) But I still don't understand some fundamental characteristics of Racket (Scheme).

  1. Why are there no 'real' variables? Why is everything a function in Racket/Scheme? Why did the language designers not include them?

  2. What is define-struct? Is it a function? Is it a class? I somehow, because of my PHP background, always think it's a class, but that can't be really correct.

My question here is I want to understand the concept of the language. I personally still think it's really strange and not like anything I worked with before, so my brain tries to compare it with JavaScript, but it just seems so different to me. Parallels/differences to JavaScript would help a lot!

like image 627
wowpatrick Avatar asked Apr 10 '12 11:04

wowpatrick


People also ask

Can you define variables in struct?

The structure members can have any variable types except type void , an incomplete type, or a function type. A member cannot be declared to have the type of the structure in which it appears.

What is a struct used for?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).

What is a struct rust?

Keyword structA type that is composed of other types. Structs in Rust come in three flavors: Structs with named fields, tuple structs, and unit structs.


2 Answers

  1. There are 'real' variables in Racket. For example, if you write this code

    (define x 3)
    

    the 'global' variable x will be set to value 3. If you now write

    (set! x 4)
    

    the variable x will change its value to 4. So, in Racket you can have a 'normal' variables like in any 'normal' language, if you want. The fact is that in Racket the preferred programming style is functional as opposed to procedural. In functional programming style variable mutation is discouraged.

  2. define-struct is a Racket macro that you use to define 'structure template' along with several other things. For example, if you write:

    (define-struct coord (x y))
    

    you just defined a 'structure template' (i.e user type named coord that have two "slots": x and y). After that, you can now:

    • create new "instance" of structure coord, for example like this: (make-coord 2 3)

    • extract slot value from the structure object:

      (coord-x (make-coord 2 3)) ;will return 2
      

      or

      (coord-y (make-coord 2 3)) ;will return 3
      
    • you can ask if some given object is just that structure. For example, (coord? 3) will return #f, since 3 is not of type coord structure, but

      (coord? (make-coord 2 3)) ;will return #t
      
like image 118
Racket Noob Avatar answered Sep 28 '22 16:09

Racket Noob


Perhaps the most popular or in-fashion way to program (using languages like C++, Javascript, and Java) has a few characteristics. You may take them for granted as self-evident, the only possible way. They include:

  • Imperative

You focus on saying "do this step, then this next step" and so on.

  • Using mutation.

You declare a variable, and keep assigning it different values ("mutate it").

  • Object-oriented.

You bundle code and data into classes, and declare instances of them as objects. Then you mutate the objects.

Learning Scheme or Racket will help you understand that these aren't the only way to go about it.

It might make your brain hurt at first, in the same way that a philosophy class might cause you to question things you took for granted. However unlike the philosophy class, there will be some practical pay-off to making brain hurt. :)

An alternative:

  • Functional (instead of imperative). Focus on expressions that return values, instead of making to-do lists of steps.
  • Immutable. Ditto.
  • Not object oriented. Using objects of classes can be a good approach to some problems, but not all. If you want to bundle code with data, there are some more general ways to go about it, such as closures with "let over lambda" and so on. Sometimes you don't need all the "baggage" of classes and especially inheritance.

Scheme and Racket make it easy to explore these ideas. But they are not "pure functional" like say Haskell, so if you really want to do imperative, mutable, object-oriented things you can do that, too. However there's not much point in learning Racket to do things the same way you would in Javascript.

like image 32
Greg Hendershott Avatar answered Sep 28 '22 16:09

Greg Hendershott