0% found this document useful (0 votes)
40 views4 pages

Understanding Scheme Functions and Variables

The document discusses various aspects of programming in Scheme, including a functional approach to problem-solving, variable declaration and initialization, and data types. It provides examples of arithmetic operations and built-in functions, as well as how to load Scheme code from a text file. Key primitive types include characters, strings, Booleans, integers, and various number types, with lists being a fundamental composite data type.

Uploaded by

lijaf37417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Understanding Scheme Functions and Variables

The document discusses various aspects of programming in Scheme, including a functional approach to problem-solving, variable declaration and initialization, and data types. It provides examples of arithmetic operations and built-in functions, as well as how to load Scheme code from a text file. Key primitive types include characters, strings, Booleans, integers, and various number types, with lists being a fundamental composite data type.

Uploaded by

lijaf37417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

; Value: 8

8.6. Evaluation

 The functional approach sometimes requires us to


take a "bottom-up" view of the problem: creating
functions to compute the lowest layer of values,
then other functions taking those as operands.

 Example: Design a code to compute (a + b + c) / (x


+ y + z)

 Compute the numerator and denominator


separately,

; for the numerator


(+ a b c)
; for the denominator
(+ x y z)
and then decide how to apply division with those
two functions as operands, i.e.:
(/ (+ a b c) (+ x y z))

8.7. Storing and using Scheme code

The load function is available to load a Scheme


program stores in a an text file, e.g.:

> (load "[Link]")

; Loading "[Link]" -- done


A. Bellaachia Page: 17
8.8. Variables

 Variables are always bound to values


 To declare and initialize a variable, we use the built in
define command, giving it the variable name and the value
it is to be initialized with (the value may be an expression)

Examples:
> (define x 3)

; Value:x

> (define foo (+ 4 7))

; Value: foo

Check the content of a variable:

>x

; Value: 3

>foo

; Value: 11

A. Bellaachia Page: 18
8.9. Data types
Literals are described as self-evaluating, in that
evaluating the literal returns the value they
represent. (E.g. evaluating 3 returns the
integer value 3.)
The primitive types are:
characters
strings (in double-quotes)
Booleans:
True: #t
False: The empty set for false or
#f (see example below).
Integers
rational numbers
real numbers
complex numbers.

List: There is also a composite data type,


called the list, which is a fundamental part
of Scheme. Lists are considered in detail in a
later section.
 Numbers
There are integers, rationals, reals, and complex
numbers.
In general, Scheme will return as exact an answer as it
can (i.e. it will give an exact integer or rational over a
real approximation).
Examples:
Let's see the results of some basic arithmetic:
>(/ 3.2 1.6)

A. Bellaachia Page: 19
; Value: 2.

>(/ 16 10)

; Value: 8/5

Suppose we were to try some comparisons:


>(< 2 3)

; Value: #t

>(< 4 3)

; Value: ()

8.10. Arithmetic functions

There are many built-in arithmetic functions. Some of


the commonly used ones include:
max, min
+, *, -, /
quotient, modulo, remainder
ceiling, floor, abs, magnitude, round, truncate
gcd, lcm
exp, log, sqrt
sin, cos, tan
There are also a number of comparison
operators returning Boolean values

A. Bellaachia Page: 20

Common questions

Powered by AI

Scheme emphasizes returning the most exact result possible in numeric operations, prioritizing integers and rational numbers over real approximations when appropriate. This exactness extends to its handling of various primitive numeric types—integers, rationals, reals, and complex numbers—and affects operations such as division, which can return an exact rational like 8/5 rather than a floating-point number when appropriate .

A literal is considered self-evaluating in Scheme if its evaluation directly yields its value without requiring further computation. Examples of self-evaluating literals include simple numeric values or booleans, such as '3', which evaluates to the integer 3, and '#t' which evaluates to the boolean true. This property simplifies evaluation, making literals directly usable in expressions .

Scheme ensures numerical precision by preferring exact values over approximations, heavily utilizing its variety of number types like integers, rationals, and complex numbers. This precision is evident in operations that might default to rational exactly where other languages might approximate. By returning results such as 8/5 for a division rather than a floating-point approximation, Scheme preserves mathematical integrity in computational contexts .

Scheme evaluates comparisons using Boolean logic, returning '#t' for true when conditions are met and '()' or '#f' for false when they are not. For example, a comparison such as (< 2 3) yields '#t', whereas (< 4 3) results in '()', reflecting Scheme's flexible interpretation of false values. This approach allows developers to utilize conditional logic seamlessly within functional workflows .

The functional approach in programming involves breaking down problems into smaller, manageable functions that compute specific components. Particularly in Scheme, this is exemplified by creating distinct functions to handle separate arithmetic operations, such as computing the numerator and denominator individually in expressions. This 'bottom-up' approach allows the division to be applied as a higher-level operation with already computed operands, like in the expression (/ (+ a b c) (+ x y z)).

Lists in Scheme function as composite data types distinct from primitive types like integers or booleans. They are crucial for handling collections of elements, enabling complex data manipulations beyond what's feasible with primitive types. Scheme lists follow a parenthetical structure allowing for nested and recursive data formations, essential for implementing functional paradigms .

Scheme's load function is crucial for managing programs by allowing developers to load and execute Scheme code stored in text files. This capability enhances code modularity and reusability, facilitating iterative development. The syntax, simply (load "myfile.txt"), executes the code within the specified file, supporting efficient testing and application of external code modules .

Scheme's rich set of arithmetic functions vastly enhances its computational capabilities. Basic operations include addition, subtraction, multiplication, and division, allowing for immediate arithmetic computation. Beyond these, specialized functions like 'quotient', 'modulo', 'gcd', and 'lcm' address more complex mathematical needs, while trigonometric and exponential functions (e.g., 'exp', 'log') support advanced numerical analyses, catering to diverse scientific and engineering applications .

Variables in Scheme provide a means to bind values to identifiers, simplifying code reuse and readability. Variables are declared and initialized using the 'define' command, where an identifier is linked to a specific value or expression outcome. For example, (define x 3) sets the variable x to 3, and (define foo (+ 4 7)) evaluates to 11, binding the result to foo. Accessing these variables later simply returns their value, aiding in efficient program execution .

Comparison operations in Scheme have practical applications in scenarios requiring decision-making processes or sorting algorithms. Functions like '<', '>', and '=' help determine control flow by assessing relationships between variables. This capability can manage program execution paths, implement conditions in algorithms, or orchestrate data sorting and filtering, leveraging Scheme's Boolean return values to dictate logical sequences in diverse computational tasks .

You might also like