Do declarations like haskell exist like python class / function documentation?

I am using the "Learn you a haskell tutorial" and have come to the type declarations section. I understand that they change the way GHCI provides error messages, but do they also affect how the actual function works? If not, then it essentially looks like a python function documentation written with "" "" "under" def someFunction (x): "? Is just an example

Code example:

removeNonUppercase :: [Char] -> [Char]  
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]

EDIT: I ask about this because the tutorial explains that haskell is a type of output at compile time.

+4
source share
3 answers

( ). , , , . :

add x y = x + y

addInt :: Int -> Int -> Int
addInt x y = x + y
*Main> :t add
add :: Num a => a -> a -> a
*Main> add 2 3
5
*Main> add 2.1 3.1
5.2
*Main> :t addInt
addInt :: Int -> Int -> Int
*Main> addInt 2 3
5
*Main> addInt 2.1 3.1 -- addInt will not accept non-Ints.

<interactive>:23:8:
    No instance for (Fractional Int) arising from the literal ‘2.1’
    In the first argument of ‘addInt’, namely ‘2.1’
    In the expression: addInt 2.1 3.1
    In an equation for ‘it’: it = addInt 2.1 3.1

, , ( ) , , , , .

, . , :

readAndShow s = show (read s)

- ...

Foo.hs:6:17:
    No instance for (Show a0) arising from a use of ‘show’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance (GHC.Arr.Ix a, Show a, Show b) => Show (GHC.Arr.Array a b)
        -- Defined in ‘GHC.Arr’
      instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
      instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
        -- Defined in ‘GHC.Real’
      ...plus 26 others
    In the expression: show (read s)
    In an equation for ‘readAndShow’: readAndShow s = show (read s)

Foo.hs:6:23:
    No instance for (Read a0) arising from a use of ‘read’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance (GHC.Arr.Ix a, Read a, Read b) => Read (GHC.Arr.Array a b)
        -- Defined in ‘GHC.Read’
      instance Read a => Read (Maybe a) -- Defined in ‘GHC.Read’
      instance (Integral a, Read a) => Read (GHC.Real.Ratio a)
        -- Defined in ‘GHC.Read’
      ...plus 25 others
    In the first argument of ‘show’, namely ‘(read s)’
    In the expression: show (read s)
    In an equation for ‘readAndShow’: readAndShow s = show (read s)
Failed, modules loaded: none.

... . read a String , show . , read s, , String as. ...

readAndShowAsInt s = show (read s :: Int)
*Main> readAndShowAsInt "2"
"2"

... - :

readAndAdd :: String -> Int -> Int
readAndAdd s y = read s + y
*Main> readAndAdd "2" 3
5
+8

, Haskell , + . :

(1) , . .

squareInt :: Int -> Int
squareInt x = x*x

Num t => t -> t. Int s, . .

(2) , , . , n :

f :: (forall x. Show x => y) -> (Int, Bool) -> (y, y)
f g (i, b) = (g i, g b)

Haskell " ", , .

+4

, . Haskell - , , , , . , Int → Int → Int, , (Int, Int) → Int, - .

They also act as a form of documentation for the programmer. It is sometimes very useful to see the type of function when writing code. Often (if functions are well-named), you can figure out what a function should do by simply looking at its name and type. The documentation is still very useful, but when you start writing and reading more functional programs, types become something useful for understanding the code.

+2
source

Source: https://habr.com/ru/post/1598954/


All Articles