Can someone try and explain these two functions: "define-type" and "type-case" in the PLAI scheme in racket? I'm a noob programmer and I don't really understand the documentation on the racket website. If anyone could provide examples, it would greatly be appreciated. Thanks.
Racket - lang plai - define-type and type-case explanations
2.7k Views Asked by indianhottie At
2
There are 2 best solutions below
0
On
Here is a little example of how to use define-type and type-case:
#lang plai
; A ListOfNumbers are either
; is either an empty list of numbers
; or is constructed to two things a, and, d,
; where a is a number and d is a list of numbers.
(define-type ListOfNumbers
(Empty)
(Cons (a number?) (d ListOfNumbers?)))
; construct a list of numbers as an example
(define a-list (Cons 42 (Cons 43 (Empty))))
a-list ; prints: (Cons 42 (Cons 43 (Empty)))
(type-case ListOfNumbers a-list
(Empty () "the list is empty")
(Cons (a d) (~a "the first number in the list is " a)))
; prints: "the first number in the list is 42"
I'm not super experienced with Lisp/Scheme/Racket, but it looks like this question is still unanswered after 5 years, so I'll give it a shot.
First of all, note that not everything is a function. For example, when you use
defineto define a function or some other value,defineis not acting as a function. A function is something that takes some input, and then returns some output.definedoes not do this. Instead, it changes the environment that you're programming in such a way that a new name exists that can be used to refer to some value.So for example, in...
...
definemodifies the programing environment so that the functioncadrnow exists.cadris a function (if you invoke it with some input, it will yield some output), butdefineitself is not a function (you're not invokingdefinewith some input in order to get some output).With that distinction hopefully cleared up,
define-typeis not a function. It is similar todefinein that it modifies the programming environment to make it so that you have new names to refer to certain values. It is used to define a new type, along with some functions the allow you to work with that type.An example taken from the Racket documentation:
Here it defines a new type
Shapewhich it says has two variants:circleandrectangle. It further says that in the case of thecirclevariant, the interesting piece of data is itsradius, which is a number; and in therectanglevariant, there's two pieces of data (or "fields"), which are itswidthandheight(both numbers).It then defines a new function
area, which is expected to take a single input of typeShape(the type we just declared earlier). Thetype-caseexpression is used to specify how to compute the area of aShapedepending on which variant we're dealing with. If we're dealing with acirclethen we can compute the area by squaring the radius and multiplying it by Pi. If we're dealing with arectangle, then we can compute the area by multiplying its width by its height.Earlier, I said
define-typeis not a function, but by virtue of using it, it defines a new type and a bunch of functions that allow us to work with that type. So what are these new functions it defines? See this example:Here we then use
defineto modify the programming environment so that the namecrefers to a circle we created with radius 10.circle?is a function that automatically got created when we did thedefine-typein the earlier example, and it returns whether or not theshapewe're dealing with is acirclevariant (as opposed to arectanglevariant). Similar, thecircle-radius,rectangle-widthandrectangle-heightfunctions were automatically defined for us when we useddefine-type, which allow us to access the fields inside of the data type.