What is define-struct in Racket and why are there no variables?
functional-programming, racket, scheme
Solution
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.
`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
Problem
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). Why are there no 'real' variables? Why is everything a function in Racket/Scheme? Why did the language designers not include them? 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!