Saturday, May 18, 2013

HASKELL MAP INTRODUCTION

HASKELL MAP INTRODUCTION

REVISED: Wednesday, February 7, 2024




Haskell Map.

I.  HASKELL MAP

A. HASKELL MAP EXAMPLES

1. Generic Haskell map Functions

-- Haskell Map List Comprehension
map        :: (a -> b) -> [a] -> [b]
map f xs = [f x | x <- xs]

-- Haskell Map Recursion
map              :: (a -> b) [a] -> [b]
map f []        = []
map f (x:xs) = f x : map f xs

2. Haskell Function Composition

Composition is associative.

"Copy Paste" the following function into your text editor and then "File, Save As" fComp.hs to your working directory:

-- Haskell Map List Composition
fComp :: [Integer] -> Integer
fComp = foldr (+) 0 . map (^ 2) . filter (> 0)

Load the above function into GHCi:

Prelude>  :load fComp
[1 of 1] Compiling Main             ( fComp.hs, interpreted )
Prelude>

From GHCi call the function:

Ok, modules loaded: Main.
Prelude>  fComp [1,2,-3,4]  -- Right to left > 0, [1,2,4], squared, [1,4,16], +, 21. 
21
it :: Integer
Prelude>  

3. Haskell "Squares" Functions

-- Haskell List Comprehension squares
squaresComp      :: [Int] -> [Int]
squaresComp xs = [ x*x | x <- xs ]

-- Haskell Recursion squares
squaresRec            :: [Int] -> [Int]
squaresRec []        = []
squaresRec (x:xs) = x*x : squaresRec xs

-- Haskell Map squares
squaresMap      :: [Int] -> [Int]
squaresMap xs = map squaresMap xs
    where
    squaresMap x = x*x

4. Haskell ord Functions

-- Haskell List Comprehension ord
ordComp      :: [Char] -> [Int]
ordComp xs = [ ord x | x <- xs ]

-- Haskell Recursion ord
ordRec            :: [Char] -> [Int]
ordRec []        = []
ordRec (x:xs) = ord x : ordRec xs

-- Haskell Map ord
ordMap      :: [Char] -> [Int]
ordMap xs = map ord xs

5. Abstraction Principle Example

The Abstraction Principle says nothing should be duplicated. We want to abstract out the commonality in our programs so we do not repeat our code. The myMap function is an example of the Abstraction Principle.

"Copy Paste" the following function into your text editor and then "File, Save As" myMap.hs to your working directory:

myMap              :: (a -> b) -> [a] -> [b]
myMap   _ []     = []
myMap f (x:xs) = f x : myMap f xs

Load the function into GHCi:

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

From GHCi call the function as follows:

Prelude>  myMap (+5) [-5,7,11]
[0,12,16]
Prelude> 

Prelude>  myMap abs [-5,7,11]
[5,7,11]
Prelude> 

Prelude>  myMap (^3) [-5,7,11]
[-125,343,1331]
Prelude>    

Prelude>  myMap sqrt [4,16,49]
[2.0,4.0,7.0]
Prelude>  

II.  CONCLUSION

In this tutorial, you have been introduced to the Haskell map function.

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.