Sunday, May 19, 2013

HASKELL BINDING AND SCOPE INTRODUCTION

HASKELL BINDING AND SCOPE INTRODUCTION

REVISED: Wednesday, October 2, 2024




Haskell binding and scope.

Local bindings are values inside of a function that only that function can see.

I. HASKELL BINDING AND SCOPE

A. HASKELL BINDING AND SCOPE EXAMPLES

1. Variables in a Haskell where clause

The function fA1, shown below, is defined at the top level; and its scope is the program, it can be used anywhere in the program.

The scope of the functions y and z, shown below, is the function fA1; they have no value outside of the function fA1

The scope of the variable x is the function fA1 which includes the where clause; x has no value outside of the function fA1.  The variable x only has meaning while the function fA1 is being applied to the variable x.

fA1 :: Num a => a -> a
fA1 x = z
      where
      y = x + 1
      z = x + y * y

Prelude>  :load fA1
[1 of 1] Compiling Main             ( fA1.hs, interpreted )
Ok, modules loaded: Main.
Prelude>

Prelude>  fA1 2
11
it :: Num a => a
Prelude>

Steps:
fA1 2
y = x + 1 therefore 
y  = 2  + 1 = 3
z = x + y * y therefore
z = 2 + 3 * 3 = 11

2. Functions in a Haskell where clause

The function fA2 is defined at the top level; and its scope is the program, it can be used anywhere in the program.

The scope of the function g is the function fA2; g has no value outside of the function fA2.

The scope of the variable x is the function fA2 which includes the where clause; x has no value outside of the function fA2

The scope of the variable y is the function g; y has no value outside of the function g.

fA2 :: Num a => a -> a
fA2 x = g (x+1)   -- Actual implied y is (x + 1).
      where
      g y = x+y*y   -- Formal y is bound to actual (x + 1).

Prelude>  :load fA2
[1 of 1] Compiling Main             ( fA2.hs, interpreted )
Ok, modules loaded: Main.
Prelude>

Prelude>  fA2 2
11
it :: Num a => a
Prelude>  

Steps:
fA2 2
fA2 x = g (x+1) therefore
fA2 2 = g (2+1) = g (3)   -- Actual implied y
g y = x+y*y
g 3 = 2 + 3 * 3 = 11

3. Haskell lambda functions

Below are two lambda functions, their x variables do not share scope. An x variable in one lambda function is not the same x variable in the other lambda function.

Binding occurrence of function x.

Binding occurrence of function x.

fA3 :: [Int] -> [Int]
fA3 xs = map (\x -> x*x) (filter (\x -> x >0) xs)

Prelude>  :load fA3
[1 of 1] Compiling Main             ( fA3.hs, interpreted )
Ok, modules loaded: Main.
Prelude>

Prelude>  fA3 [1,2,-3]
[1,4]
it :: [Int]
Prelude>  

II. CONCLUSION

In this tutorial, you have been introduced to Haskell binding and scope.

III. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.