Instantly share code, notes, and snippets.

Last active June 17, 2018 23:07
Star You must be signed in to star a gist

## chapter 3 - Types and classes

A type is a collection of related values

For example type Bool

True

Bool

False

Bool

¬

Bool → Bool

In Haskell, every expression must have a type, which is calculated prior to evaluating the expression by process called type inference

:info negate

¬ False

Bool

¬

Bool → Bool

False

Bool

Erroz ¬3

Why?

3

Bool

Dont eliminate other kinds of error, for example:

1 div 0 is undefined

:type not :type not False :type not 3

### Basic type

Bool - Logical values Char - single characters String - strings of characters Int - fixed-precision integers Integer - arbitrary-precision integers Float - single-precision floating-point numbers

We conclude this section by noting a single number may have more than one numeric type. For example, 3

Int, 3 :: Integer, and 3 :: Float are all valid typings for the number 3

### Other types

• List types

• A list is a sequence of elements of the same type. [T]

``````    [False, True, False]
['a','b','c']
["Hola","inmundo"]

[['a','b'],['c']]``````
• Tuple types

• A tuple is a finite sequence of components of possibly different types (T1, T2, …​ Tn)

``````    (False,True)
(False,'a')
(False,'a',"Yes")

(['a','b'],[False,True])``````
• Function type

• A function is a mapping from arguments of one type to results of another type

``````:type negate
:type isDigit

Errorz Note that there is no restriction that functions must be total on their argument type, in the sense that there may be some arguments for which the result is not defined. `head []`

• Curried functions

``````  add' x y = x + y

add' :: Int -> (Int -> Int)``````
• Polymorphic types

• The library function length calculates the length of any list, irrespective of the type of the elements of the list.

``````lenght [1,2,3]
lenght ["YES","No"]
lenght [isDigit,isLower,isUpper]``````

The idea that length can be applied to lists whose elements have any type is made precise in its type by the inclusion of a type variable.

That is, for any type a, the function length has type [a] → Int. A type that contains one or more type variables is called polymorphi

``````1+2

2.1+2.2``````

The idea that + can be applied to numbers of any numeric type is made precise in its type by the inclusion of a class constraint. Class constraints are written in the form C a, where C is the name of a class and a is a type variable.

:type (+)

• Basic Classes

• equality types `(==)` and `(/=)`, All the basic types Bool, Char, String, Int, Integer, and Float are instances of the Eq class, as are list and tuple types

• ordered types ``` (<) (⇐) (>) (>=) min max ```

• showable types

`:type show`

`:type read`
• numeric types - This class contains types that are instances of the equality class Eq and showable class Show, but in addition whose values are numeric

• integral types - This class contains types that are instances of the numeric class Num, but in addition whose values are integers

• fractional types - This class contains types that are instances of the numeric class Num, but in addition whose values are non-integral

## chapter 4 - Defining functions

``````isDigit :: Char -> Bool
even :: Integral a => a -> Bool
splitAt :: Int -> [a] -> ([a],[a])``````
• Conditional expressions

``````abs :: Int -> Int
abs n = if n >= 0 then n else -n

signum :: Int -> Int
signum n = if n < 0 then -1 else
if n == 0 then 0 else 1``````
• Guarded equations

``````abs n | n >= 0 = n
| otherwise = -n``````
• Pattern matching

``````(^) :: Bool -> Bool -> Bool
True ^ True = True
True ^ False = False
False ^ True = False
False ^ False = False``````

Otra hechiceria mas de haskell y JJ es chingo no se porque lo menciono ahorita pero todos los sabes y no esta mal rectificarlo de vez en cuando.

``````True ^ True = True
_ ^ _ = False``````

other result

``````b ^ c | b == c = b
| otherwise = False``````
• Tuple patterns

``````fst :: (a, b) -> a
fst (x,_) = x``````
• List patterns

``````test :: [Char] -> Bool
test['a',_,_] = True
test _ = False``````
• Integer patterns

``````pred :: Int -> Int
pred 0 = 0
pred (n+1) = n``````
• Lambda expressions - As an alternative to defining functions using equations, functions can also be constructed using lambda expressions, which comprise a pattern for each of the arguments, a body that specifies how the result can be calculated in terms of the arguments, but do not give a name for the function itself. In other words, lambda expressions are nameless functions.

(lambda x → x + x) 2 4

• Sections