Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Scheme Structures


  • Please log in to reply
5 replies to this topic

#1 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 14 August 2011 - 04:30 AM

Scheme Structures

Table of Contents
1. Scheme Functions and Constants
2. Scheme Structures
3. Scheme Defining Structures
4. Scheme Conditonals
5. Scheme Lists
6. Scheme Local Definitions








Structures provide a means of grouping related data into an entity. Most modern languages contain a context similar to structures. Languages such as C and C++ also support structures where as languages like Java and C# support classes. Note: structures are not classes. Classes are much more flexible as they support features such as information hiding, inheritance and allow you to define your own methods. Structures do not have these features.
Structures are useful for implementing data structures in Scheme.

Built-in Scheme Structures – Posn

Posn is a built-in Scheme structure that has two fields x and y. In other words, this structure is meant to represent a point on a Cartesian grid.
We create a posn like this:

(make-posn 3 2)

This is the constructor function which takes 2 parameters x followed by y. So in this case the x coordinate is 3 and the y-coordinate is 2.
It is important at this point to mention that Scheme is not a strongly typed language. It is syntactically valid to write:

(make-posn 'Hi 'CodeCall’)

But in a logistic sense this doesn’t make any sense. You may recall that a posn represents a point on a grid. (‘Hi, ‘CodeCall) doesn’t represent any point on a Cartesian grid. This is why when we write structures and functions we write a contract that the programmer must adhere to. If the contract is broken then bad things can happen. We want to write our programs so that if the contract is adhered to then the program will function properly.
We can write checks to ensure that types passed to our function are valid if we want. We will see how to do this in a later tutorial.

Now let us look at selectors.

Selectors

When a structure is created a selector function is created for every field. The selector function takes the form: (structname-fieldname struct)
So in the posn example if we want the x-value we can do the follow:

 (posn-x posn) ;; returns the x value of the posn
 (posn-y posn) ;; returns the y value of the posn

Example:

(posn-x (make-posn 3 2))

This code produces the x value of the posn which in this case is 3. If we did (posn-y (make-posn 3 2)) the value produced would be 2.

Predicate function

Another function created when we create a structure is a predicate function named struct-name? which returns true if a value is that particular structure. As we will see later this is useful when dealing with recursive structures.

Example:

(posn? 5) ;; false
 (posn? (make-posn  3 2));; true

Next time we look at creating our own structures! Any questions, post below! :)

Table of Contents
1. Scheme Functions and Constants
2. Scheme Structures
3. Scheme Defining Structures
4. Scheme Conditonals
5. Scheme Lists
6. Scheme Local Definitions





Edited by chili5, 21 August 2011 - 06:11 AM.
added selector examples and table of contents

  • 1

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 16 August 2011 - 10:51 AM

So
(posn-x (make-posn 3 2))
would return 3?
  • 0

#3 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 16 August 2011 - 02:02 PM

Yep! :)
  • 0

#4 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 17 August 2011 - 12:46 PM

Added examples of the selectors and a table of contents for future use.
  • 0

#5 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts
  • Programming Language:C, Java, C++, Python, Ruby, Scheme, Bash, Haskell, Others
  • Learning:Others

Posted 24 April 2012 - 05:37 AM

I might be a bit late to the party, but I feel the need to point out that everything in this post (and the follow-up post about defining structs) applies to Racket (formerly PLT Scheme) only. Neither posn nor the concept of structs are part of any Scheme standard.
  • 0

#6 UniBrain

UniBrain

    CC Regular

  • Validating
  • PipPipPip
  • 48 posts
  • Programming Language:C, Java, C++, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Pascal, Assembly, Scheme, Prolog, Others
  • Learning:Others

Posted 06 May 2012 - 06:21 PM

thanks went through all your tutorials in about 1 hr using DrRacket, enjoyed it
  • 0

I wish nothing for myself; I wish solely that this consumptive existence is realigned... Such is perhaps attainable with science an technology... If cosmological constants yielded an energy transference bounded universe, by modus ponens, it perhaps follows that such constants are affixable... This universe is but a program whose underlying construct is starkly outdated....





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download