•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Scheme Functions and Constants

4 replies to this topic

### #1 chili5

chili5

CC Mentor

• Expert Member
• 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:03 AM

Scheme Functions

Scheme, is a functional programming language. A functional programming language is based on the use of functions and computing new values instead of mutating old variables. One of the main features of functional programming languages is functions are values. For this reason, functions can be parameters to a function and a function can also produce a function.

As we will see this has many practical issues in programming including simulating structures and creating abstract functions.

Creating Functions

We create functions using the define keyword.

Example:
```(define (add-numbers x y)
(+ x y))
```

This function is called add-numbers and takes 2 parameters x and y and produces the value (+ x y). Scheme uses prefix notation instead of infix notation as most modern languages use. This means that the operator comes before the operands.

For example,
(+ 3 2) is equivalent to 3 + 2.

In Scheme, the parentheses around an expression are required. They are used to indicate an expression not to dictate order of operations. An expression in general takes the form (operation operand1 operand2 .... operationx) when operation takes x parameters. A note the + operator takes an unlimited number of parameters.

Order of Operations

The parenthesis is required and expressions in the inner most expressions are evaluated first and then parenthesis are not required for order of operations.

Example:
(* (+ (- 5 3) 2) 4)
The expression (- 5 3) is evaluated first which produces the value 2. Then the expression becomes
(* (+ 2 2) 4)
Then (+ 2 2) is evaluated next and then the expression becomes (* 4 4) which simplifies to 16.
This makes the value of the entire expression 16 when fully evaluated.

Constants

Scheme DOES NOT support the use of variables. The reasoning here is functional programming is based on the use of recursion to make new values not mutate old ones. However, sometimes we may desire variables to make code cleaner. In this case what do we do?
Consider this code:

```(define (grade-calculation mark1 mark2 mark3)
(+ (* mark1 0.40)
(* mark2 0.20)
(* mark3 0.40)))
```

This function calculates a weighted mark of three marks. It works but what is the problem? The problem here is the code doesn’t explain nicely what 0.4 and 0.2 represent. Is the first 0.4 the same as the second 0.4 or are they different contexts? One could be assignments are worth 40% and the other could be exams are 40%. We just don’t know from the context the meaning of these numbers. We can solve this problem using constants.

Constants are defined as follows:

(define CONSTANT_NAME CONSTANT_VALUE)

So we can rewrite the above function as:
```(define assignment-weight 0.4)
(define exam1-weight 0.2)
(define exam2-weight 0.4)

(+ (* mark1 assignment-weight)
(* mark2 exam1-weight)
(* mark3 exam2-weight)))
```

This function is so much easier to understand with the constants added. When we need to modify the program it is easy to modify since all our constants will be declared in one location (preferably at the top of the code file).
It is important to realize that the constants MUST be declared outside the function. You can only have ONE statement in your function. We will see later ways to get around this problem.

As always, post if you have ANY questions.

Edited by chili5, 21 August 2011 - 06:10 AM.

• 0

### #2 John

John

CC Mentor

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

Posted 16 August 2011 - 10:42 AM

VERY NICE! I'm really happy to see some tutorials on less common programming languages. Keep up the good work.
• 0

### #3 chili5

chili5

CC Mentor

• Expert Member
• 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 - 12:48 PM

Got lots more Scheme planned. I didn't originally like it when I got introduced to it but after awhile I really loved it. It has A LOT of amazing features in it.
• 0

### #4 John

John

CC Mentor

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

Posted 16 August 2011 - 01:23 PM

Can't wait! I've played around with a few functional languages, but I haven't come across the need to do any serious development with any of them.
• 0

### #5 chili5

chili5

CC Mentor

• Expert Member
• 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 30 August 2011 - 07:28 AM

JavaScript is actually a functional programming language. With functional ideas you can make some simple classes in JS like this:

```function MyClass () {
var privateVar;
this.MyMethod = function()  { …};
};

var x = new MyClass();

```

The idea is everytime you do new MyClass() you get a new instance of a function.

What's a Closure?
• 0

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