User Tools

Site Tools


codesnippets:binding

This is an old revision of the document!


Binding

A binding binds a name or even several names with a function definition.

In Haskell, a binding is defined by symbol “=” whereas

  1. on the left hand side
    1. the function name and its parameters
    2. or a pattern is given,
  2. and on the right had side the definition of the function.

1) with function name and parameter

Example

half x = x / 2

…works in…

module Main where
 
main :: IO ()
main = 
        do
                print $ half 5
 
half x = x / 2

2) with pattern matching

Example 1

(x1, x2, x3) = (42, "Hello", "World")

…works in…

module Main where
 
main :: IO ()
main = 
        do
                print x1
                print x2
                print x3
 
(x1, x2, x3) = (42, "Hello", "World")

Example 2

Person name number = Person "Lutz" 1243

…works in…

module Main where
 
main :: IO ()
main = 
        do
                print name
                print number
 
data Person = Person { sName :: String, iNumber :: Integer }
 
Person name number = Person "Lutz" 1243

Example 3

(x1, Person x2 x3, x4) = ("...", Person "Lutz" 1243, '#')

…works in…

module Main where
 
main :: IO ()
main = 
        do
                print x1
                print x2
                print x3
                print x4
 
data Person = Person { sName :: String, iNumber :: Integer }
 
(x1, Person x2 x3, x4) = ("...", Person "Lutz" 1243, '#')
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
codesnippets/binding.1616511636.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki