Category Archives: Haskell

Haskell – 62 – sintassi nelle funzioni – 2

Continuo da qui, copio qui.

Guardie
Whereas patterns are a way of making sure a value conforms to some form and deconstructing it, guards are a way of testing whether some property of a value (or several of them) are true or false. That sounds a lot like an if statement and it’s very similar. The thing is that guards are a lot more readable when you have several conditions and they play really nicely with patterns.

Instead of explaining their syntax, let’s just dive in and make a function using guards. We’re going to make a simple function that berates you differently depending on your BMI (body mass index). Your BMI equals your weight divided by your height squared. If your BMI is less than 18.5, you’re considered underweight. If it’s anywhere from 18.5 to 25 then you’re considered normal. 25 to 30 is overweight and more than 30 is obese. So here’s the function (we won’t be calculating it right now, this function just gets a BMI and tells you off)

bmi.hs

bmiTell :: (RealFloat a) => a -> String
bmiTell bmi
  | bmi <= 18.5 = "You're underweight, you emo, you!"
  | bmi <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!"
  | bmi <= 30.0 = "You're fat! Lose some weight, fatty!"
  | otherwise   = "You're a whale, congratulations!"

Guards are indicated by pipes that follow a function’s name and its parameters. Usually, they’re indented a bit to the right and lined up. A guard is basically a boolean expression. If it evaluates to True, then the corresponding function body is used. If it evaluates to False, checking drops through to the next guard and so on. If we call this function with 24.3, it will first check if that’s smaller than or equal to 18.5. Because it isn’t, it falls through to the next guard. The check is carried out with the second guard and because 24.3 is less than 25.0, the second string is returned.

This is very reminiscent of a big if else tree in imperative languages, only this is far better and more readable. While big if else trees are usually frowned upon, sometimes a problem is defined in such a discrete way that you can’t get around them. Guards are a very nice alternative for this.

Many times, the last guard is otherwise. otherwise is defined simply as otherwise = True and catches everything. This is very similar to patterns, only they check if the input satisfies a pattern but guards check for boolean conditions. If all the guards of a function evaluate to False (and we haven’t provided an otherwise catch-all guard), evaluation falls through to the next pattern. That’s how patterns and guards play nicely together. If no suitable guards or patterns are found, an error is thrown.

Of course we can use guards with functions that take as many parameters as we want. Instead of having the user calculate his own BMI before calling the function, let’s modify this function so that it takes a height and weight and calculates it for us.

bmi-1.hs

bmiTell :: (RealFloat a) => a -> a -> String
bmiTell weight height
  | weight / height ^ 2 <= 18.5 = "You're underweight, you emo, you!"
  | weight / height ^ 2 <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!"
  | weight / height ^ 2 <= 30.0 = "You're fat! Lose some weight, fatty!"
  | otherwise = "You're a whale, congratulations!"

Let’s see if I’m fat …

*Main> :l bmi-1
[1 of 1] Compiling Main             ( bmi-1.hs, interpreted )
Ok, modules loaded: Main.
*Main> bmiTell 95 1.80
"You're fat! Lose some weight, fatty!"

Yes, I know. But Haskell just called me fatty. Whatever!

Note that there’s no = right after the function name and its parameters, before the first guard. Many newbies get syntax errors because they sometimes put it there.

Another very simple example: let’s implement our own max function. If you remember, it takes two things that can be compared and returns the larger of them.

max' :: (Ord a) => a -> a -> a
max' a b
  | a > b     = a
  | otherwise = b

Guards can also be written inline, although I’d advise against that because it’s less readable, even for very short functions. But to demonstrate, we could write max' like this:

max' :: (Ord a) => a -> a -> a
max' a b | a > b = a | otherwise = b

Ugh! Not very readable at all! Moving on: let’s implement our own compare by using guards.

comp.hs

myCompare :: (Ord a) => a -> a -> Ordering
a `myCompare` b
  | a > b     = GT
  | a == b    = EQ
  | otherwise = LT
*Main> :l comp
[1 of 1] Compiling Main             ( comp.hs, interpreted )
Ok, modules loaded: Main.
*Main> 3 `myCompare` 2
GT
*Main> 3 `myCompare` 5
LT
*Main> myCompare 8 8
EQ

Note: Not only can we call functions as infix with backticks, we can also define them using backticks. Sometimes it’s easier to read that way.

🤩

Annunci

Haskell – 61 – sintassi nelle funzioni – 1

Continuo da qui, copio qui.

Pattern matching
No, non so come tradurlo, ho carenze linguistiche 😐

This chapter will cover some of Haskell’s cool syntactic constructs and we’ll start with pattern matching. Pattern matching consists of specifying patterns to which some data should conform and then checking to see if it does and deconstructing the data according to those patterns.

When defining functions, you can define separate function bodies for different patterns. This leads to really neat code that’s simple and readable. You can pattern match on any data type — numbers, characters, lists, tuples, etc. Let’s make a really trivial function that checks if the number we supplied to it is a seven or not.

lucky.hs

lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry, you're out of luck, pal!"
Prelude> :l lucky
[1 of 1] Compiling Main             ( lucky.hs, interpreted )
Ok, modules loaded: Main.
*Main> lucky 5
"Sorry, you're out of luck, pal!"
*Main> lucky 7
"LUCKY NUMBER SEVEN!"

When you call lucky, the patterns will be checked from top to bottom and when it conforms to a pattern, the corresponding function body will be used. The only way a number can conform to the first pattern here is if it is 7. If it’s not, it falls through to the second pattern, which matches anything and binds it to x. This function could have also been implemented by using an if statement. But what if we wanted a function that says the numbers from 1 to 5 and says “Not between 1 and 5” for any other number? Without pattern matching, we’d have to make a pretty convoluted if then else tree. However, with it:

sayMe.hs

sayMe :: (Integral a) => a -> String
sayMe 1 = "One!"
sayMe 2 = "Two!"
sayMe 3 = "Three!"
sayMe 4 = "Four!"
sayMe 5 = "Five!"
sayMe x = "Not between 1 and 5"
*Main> :l sayMe
[1 of 1] Compiling Main             ( sayMe.hs, interpreted )
Ok, modules loaded: Main.
*Main> sayMe 3
"Three!"
*Main> sayMe 8
"Not between 1 and 5"

Note that if we moved the last pattern (the catch-all one) to the top, it would always say “Not between 1 and 5”, because it would catch all the numbers and they wouldn’t have a chance to fall through and be checked for any other patterns.

Remember the factorial function we implemented previously? We defined the factorial of a number n as product [1..n]. We can also define a factorial function recursively, the way it is usually defined in mathematics. We start by saying that the factorial of 0 is 1. Then we state that the factorial of any positive integer is that integer multiplied by the factorial of its predecessor. Here’s how that looks like translated in Haskell terms.

fact.hs

factorial :: (Integral a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)
*Main> :l fact
[1 of 1] Compiling Main             ( fact.hs, interpreted )
Ok, modules loaded: Main.
*Main> factorial 5
120
*Main> factorial 12
479001600

This is the first time we’ve defined a function recursively. Recursion is important in Haskell and we’ll take a closer look at it later. But in a nutshell, this is what happens if we try to get the factorial of, say, 3. It tries to compute 3 * factorial 2. The factorial of 2 is 2 * factorial 1, so for now we have 3 * (2 * factorial 1). factorial 1 is 1 * factorial 0, so we have 3 * (2 * (1 * factorial 0)). Now here comes the trick — we’ve defined the factorial of 0 to be just 1 and because it encounters that pattern before the catch-all one, it just returns 1. So the final result is equivalent to 3 * (2 * (1 * 1)). Had we written the second pattern on top of the first one, it would catch all numbers, including 0 and our calculation would never terminate. That’s why order is important when specifying patterns and it’s always best to specify the most specific ones first and then the more general ones later.

Pattern matching can also fail. If we define a function like this:

charName.hs

charName :: Char -> String
charName 'a' = "Alice"
charName 'b' = "Bob"
charName 'c' = "Charlie"

and then try to call it with an input that we didn’t expect, this is what happens:

*Main> :l charName
[1 of 1] Compiling Main             ( charName.hs, interpreted )
Ok, modules loaded: Main.
*Main> charName 'b'
"Bob"
*Main> charName 'a'
"Alice"
*Main> charName 'c'
"Charlie"
*Main> charName 'd'
"*** Exception: charName.hs:(2,1)-(4,24): Non-exhaustive patterns in function charName

It complains that we have non-exhaustive patterns, and rightfully so. When making patterns, we should always include a catch-all pattern so that our program doesn’t crash if we get some unexpected input.

Pattern matching can also be used on tuples. What if we wanted to make a function that takes two vectors in a 2D space (that are in the form of pairs) and adds them together? To add together two vectors, we add their x components separately and then their y components separately. Here’s how we would have done it if we didn’t know about pattern matching:

addVecs.hs

addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)
addVectors a b = (fst a + fst b, snd a + snd b)
*Main> :l addVecs
[1 of 1] Compiling Main             ( addVecs.hs, interpreted )
Ok, modules loaded: Main.
*Main> addVectors (1, 2) (3, 4)
(4,6)

Well, that works, but there’s a better way to do it. Let’s modify the function so that it uses pattern matching.

addVecs-pm.hs

addVectors' :: (Num a) => (a, a) -> (a, a) -> (a, a)
addVectors' (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
*Main> :l addVecs-pm
[1 of 1] Compiling Main             ( addVecs-pm.hs, interpreted )
Ok, modules loaded: Main.
*Main> addVectors' (1, 2) (3, 4)
(4,6)

There we go! Much better. Note that this is already a catch-all pattern. The type of addVectors (in both cases) is addVectors :: (Num a) => (a, a) -> (a, a) - > (a, a), so we are guaranteed to get two pairs as parameters.

fst and snd extract the components of pairs. But what about triples? Well, there are no provided functions that do that but we can make our own.

fst.hs

first :: (a, b, c) -> a
first (x, _, _) = x

second :: (a, b, c) -> b
second (_, y, _) = y

third :: (a, b, c) -> c
third (_, _, z) = z

The _ means the same thing as it does in list comprehensions. It means that we really don’t care what that part is, so we just write a _.

Which reminds me, you can also pattern match in list comprehensions. Check this out:

*Main> xs = [(1,3), (4,3), (2,4), (5,3), (5,6), (3,1)]
*Main> [a+b | (a,b) <- xs]
[4,7,6,8,11,4]

Should a pattern match fail, it will just move on to the next element.

Lists themselves can also be used in pattern matching. You can match with the empty list [] or any pattern that involves : and the empty list. But since [1,2,3] is just syntactic sugar for 1:2:3:[], you can also use the former pattern. A pattern like x:xs will bind the head of the list to x and the rest of it to xs, even if there’s only one element so xs ends up being an empty list.

Note: The x:xs pattern is used a lot, especially with recursive functions. But patterns that have : in them only match against lists of length 1 or more.

If you want to bind, say, the first three elements to variables and the rest of the list to another variable, you can use something like x:y:z:zs. It will only match against lists that have three elements or more.

Now that we know how to pattern match against list, let’s make our own implementation of the head function.

myHead.hs

head' :: [a] -> a
head' [] = error "Can't call head on an empty list, dummy!"
head' (x:_) = x

Checking if it works:

*Main> :l myHead
[1 of 1] Compiling Main             ( myHead.hs, interpreted )
Ok, modules loaded: Main.
*Main> head' [4,5,6]
4
*Main> head' "Hello"
'H'

Nice! Notice that if you want to bind to several variables (even if one of them is just _ and doesn’t actually bind at all), we have to surround them in parentheses. Also notice the error function that we used. It takes a string and generates a runtime error, using that string as information about what kind of error occurred. It causes the program to crash, so it’s not good to use it too much. But calling head on an empty list doesn’t make sense.

Let’s make a trivial function that tells us some of the first elements of the list in (in)convenient English form.

tell.hs

tell :: (Show a) => [a] -> String
tell [] = "The list is empty"
tell (x:[]) = "The list has one element: " ++ show x
tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y
tell (x:y:_) = "This list is long. The first two elements are: " ++ show x ++ " and " ++ show y
*Main> :l tell
[1 of 1] Compiling Main             ( tell.hs, interpreted )
Ok, modules loaded: Main.
*Main> tell ""
"The list is empty"
*Main> tell "ciao"
"This list is long. The first two elements are: 'c' and 'i'"
*Main> tell ("ciao")
"This list is long. The first two elements are: 'c' and 'i'"
*Main> tell ["ciao"]
"The list has one element: \"ciao\""

This function is safe because it takes care of the empty list, a singleton list, a list with two elements and a list with more than two elements. Note that (x:[]) and (x:y:[]) could be rewriten as [x] and [x,y] (because its syntatic sugar, we don’t need the parentheses). We can’t rewrite (x:y:_) with square brackets because it matches any list of length 2 or more.

We already implemented our own length function using list comprehension. Now we’ll do it by using pattern matching and a little recursion:

len.hs

length' :: (Num b) => [a] -> b
length' [] = 0
length' (_:xs) = 1 + length' xs
*Main> :l len
[1 of 1] Compiling Main             ( len.hs, interpreted )
Ok, modules loaded: Main.
*Main> length' ""
0
*Main> length' "ciao Juhan!"
11
*Main> length' [1,2,3]
3

This is similar to the factorial function we wrote earlier. First we defined the result of a known input — the empty list. This is also known as the edge condition. Then in the second pattern we take the list apart by splitting it into a head and a tail. We say that the length is equal to 1 plus the length of the tail. We use _ to match the head because we don’t actually care what it is. Also note that we’ve taken care of all possible patterns of a list. The first pattern matches an empty list and the second one matches anything that isn’t an empty list.

Let’s see what happens if we call length' on "ham". First, it will check if it’s an empty list. Because it isn’t, it falls through to the second pattern. It matches on the second pattern and there it says that the length is 1 + length' "am", because we broke it into a head and a tail and discarded the head. O-kay. The length' of "am" is, similarly, 1 + length' "m". So right now we have 1 + (1 + length' "m"). length' "m" is 1 + length' "" (could also be written as 1 + length' []). And we’ve defined length' [] to be 0. So in the end we have 1 + (1 + (1 + 0)).

Let’s implement sum. We know that the sum of an empty list is 0. We write that down as a pattern. And we also know that the sum of a list is the head plus the sum of the rest of the list. So if we write that down, we get:

mySum.hs

sum' :: (Num a) => [a] -> a
sum' [] = 0
sum' (x:xs) = x + sum' xs
*Main> :l mySum
[1 of 1] Compiling Main             ( mySum.hs, interpreted )
Ok, modules loaded: Main.
*Main> sum' []
0
*Main> sum' [1,2,3]
6

There’s also a thing called as patterns. Those are a handy way of breaking something up according to a pattern and binding it to names whilst still keeping a reference to the whole thing. You do that by putting a name and an @ in front of a pattern. For instance, the pattern xs@(x:y:ys). This pattern will match exactly the same thing as x:y:ys but you can easily get the whole list via xs instead of repeating yourself by typing out x:y:ys in the function body again. Here’s a quick and dirty example:

cap.hs

capital :: String -> String
capital "" = "Empty string, whoops!"
capital all@(x:xs) = "The first letter of " ++ all ++ " is " ++ [x]
*Main> :l cap
[1 of 1] Compiling Main             ( cap.hs, interpreted )
Ok, modules loaded: Main.
*Main> capital ""
"Empty string, whoops!"
*Main> capital "Juhan"
"The first letter of Juhan is J"

Normally we use as patterns to avoid repeating ourselves when matching against a bigger pattern when we have to use the whole thing again in the function body. NOn mi è tanto chiaro quando convenga; da approfondire.

One more thing — you can’t use ++ in pattern matches. If you tried to pattern match against (xs ++ ys), what would be in the first and what would be in the second list? It doesn’t make much sense. It would make sense to match stuff against (xs ++ [x,y,z]) or just (xs ++ [x]), but because of the nature of lists, you can’t do that.

🤩

Haskell – 60 – tipi e classi di tipi – 2

Continuo da qui, copio qui.

Tipi variabili
What do you think is the type of the head function? Because head takes a list of any type and returns the first element, so what could it be? Let’s check!

Prelude> :t head
head :: [a] -> a

Hmmm! What is this a? Is it a type? Remember that we previously stated that types are written in capital case, so it can’t exactly be a type. Because it’s not in capital case it’s actually a type variable. That means that a can be of any type. This is much like generics in other languages, only in Haskell it’s much more powerful because it allows us to easily write very general functions if they don’t use any specific behavior of the types in them. Functions that have type variables are called polymorphic functions. The type declaration of head states that it takes a list of any type and returns one element of that type.

Although type variables can have names longer than one character, we usually give them names of a, b, c, d, ....

Remember fst? It returns the first component of a pair. Let’s examine its type.

Prelude> :t fst
fst :: (a, b) -> a

We see that fst takes a tuple which contains two types and returns an element which is of the same type as the pair’s first component. That’s why we can use fst on a pair that contains any two types. Note that just because a and b are different type variables, they don’t have to be different types. It just states that the first component’s type and the return value’s type are the same.

Typeclasses 101
A typeclass is a sort of interface that defines some behavior. If a type is a part of a typeclass, that means that it supports and implements the behavior the typeclass describes. A lot of people coming from OOP get confused by typeclasses because they think they are like classes in object oriented languages. Well, they’re not. You can think of them kind of as Java interfaces, only better.

What’s the type signature of the == function?

Prelude> :t (==)
(==) :: Eq a => a -> a -> Bool

Note: the equality operator, == is a function. So are +, *, -, / and pretty much all operators. If a function is comprised only of special characters, it’s considered an infix function by default. If we want to examine its type, pass it to another function or call it as a prefix function, we have to surround it in parentheses.

Interesting. We see a new thing here, the => symbol. Everything before the => symbol is called a class constraint. We can read the previous type declaration like this: the equality function takes any two values that are of the same type and returns a Bool. The type of those two values must be a member of the Eq class (this was the class constraint).

The Eq typeclass provides an interface for testing for equality. Any type where it makes sense to test for equality between two values of that type should be a member of the Eq class. All standard Haskell types except for IO (the type for dealing with input and output) and functions are a part of the Eq typeclass.

The elem function has a type of (Eq a) => a -> [a] -> Bool because it uses == over a list to check whether some value we’re looking for is in it.

Some basic typeclasses:

Eq is used for types that support equality testing. The functions its members implement are == and /=. So if there’s an Eq class constraint for a type variable in a function, it uses == or /= somewhere inside its definition. All the types we mentioned previously except for functions are part of Eq, so they can be tested for equality.

Prelude> 5 == 5
True
Prelude> 5 /= 5
False
Prelude> 'a' == 'a'
True
Prelude> "Ho Ho" == "Ho Ho"
True
Prelude> 3.432 == 3.432
True

Ord is for types that have an ordering.

Prelude> :t (>)
(>) :: Ord a => a -> a -> Bool

All the types we covered so far except for functions are part of Ord. Ord covers all the standard comparing functions such as >, <, >= and <=. The compare function takes two Ord members of the same type and returns an ordering. Ordering is a type that can be GT, LT or EQ, meaning greater than, lesser than and equal, respectively.

To be a member of Ord, a type must first have membership in the prestigious and exclusive Eq club.

Prelude> "Abrakadabra" `compare` "Zebra"
LT
Prelude> 5 >= 2
True
Prelude> 5 `compare` 3
GT

Members of Show can be presented as strings. All types covered so far except for functions are a part of Show. The most used function that deals with the Show typeclass is show. It takes a value whose type is a member of Show and presents it to us as a string.

Prelude> show 3
"3"
Prelude> show 3.1415
"3.1415"
Prelude> show True
"True"

Read is sort of the opposite typeclass of Show. The read function takes a string and returns a type which is a member of Read.

Prelude> read "True" || False
True
Prelude> read "8.2" + 3.8
12.0
Prelude> read "5" - 2
3
Prelude> read "[1,2,3,4]" ++ [3]
[1,2,3,4,3]

So far so good. Again, all types covered so far are in this typeclass. But what happens if we try to do just read "4"?

Prelude> read "4"
*** Exception: Prelude.read: no parse

A Miran, che usa una versione precedente di GHC, da un messaggio più esteso:

Prelude> read "4"
:1:0:
    Ambiguous type variable `a' in the constraint:
      `Read a' arising from a use of `read' at :1:0-7
    Probable fix: add a type signature that fixes these type variable(s)

What GHCI is telling us here is that it doesn’t know what we want in return. Notice that in the previous uses of read we did something with the result afterwards. That way, GHCI could infer what kind of result we wanted out of our read. If we used it as a boolean, it knew it had to return a Bool. But now, it knows we want some type that is part of the Read class, it just doesn’t know which one. Let’s take a look at the type signature of read.

Prelude> :t read
read :: Read a => String -> a

See? It returns a type that’s part of Read but if we don’t try to use it in some way later, it has no way of knowing which type. That’s why we can use explicit type annotations. Type annotations are a way of explicitly saying what the type of an expression should be. We do that by adding :: at the end of the expression and then specifying a type. Observe:

Prelude> read "5" :: Int
5
Prelude> read "5" :: Float
5.0
Prelude> (read "5" :: Float) * 4
20.0
Prelude> read "[1,2,3,4]" :: [Int]
[1,2,3,4]
Prelude> read "(3, 'a')" :: (Int, Char)
(3,'a')

Most expressions are such that the compiler can infer what their type is by itself. But sometimes, the compiler doesn’t know whether to return a value of type Int or Float for an expression like read "5". To see what the type is, Haskell would have to actually evaluate read "5". But since Haskell is a statically typed language, it has to know all the types before the code is compiled (or in the case of GHCI, evaluated). So we have to tell Haskell: “Hey, this expression should have this type, in case you don’t know!”.

Enum members are sequentially ordered types — they can be enumerated. The main advantage of the Enum typeclass is that we can use its types in list ranges. They also have defined successors and predecesors, which you can get with the succ and pred functions. Types in this class: (), Bool, Char, Ordering, Int, Integer, Float and Double.

Prelude> ['a'..'e']
"abcde"
Prelude> [LT .. GT]
[LT,EQ,GT]
Prelude> [3 .. 5]
[3,4,5]
Prelude> succ 'B'
'C'

Bounded members have an upper and a lower bound.

Prelude> minBound :: Int
-9223372036854775808
Prelude> maxBound :: Char
'\1114111'
Prelude> maxBound :: Bool
True
Prelude> minBound :: Bool
False

minBound and maxBound are interesting because they have a type of (Bounded a) => a. In a sense they are polymorphic constants.

All tuples are also part of Bounded if the components are also in it.

Prelude> maxBound :: (Bool, Int, Char)
(True,9223372036854775807,'\1114111')

Num is a numeric typeclass. Its members have the property of being able to act like numbers. Let’s examine the type of a number.

Prelude> :t 20
20 :: Num t => t

It appears that whole numbers are also polymorphic constants. They can act like any type that’s a member of the Num typeclass.

Prelude> 20 :: Int
20
Prelude> 20 :: Integer
20
Prelude> 20 :: Float
20.0
Prelude> 20 :: Double
20.0

Those are types that are in the Num typeclass. If we examine the type of *, we’ll see that it accepts all numbers.

Prelude> :t (*)
(*) :: Num a => a -> a -> a

It takes two numbers of the same type and returns a number of that type. That’s why (5 :: Int) * (6 :: Integer) will result in a type error whereas 5 * (6 :: Integer) will work just fine and produce an Integer because 5 can act like an Integer or an Int.

To join Num, a type must already be friends with Show and Eq.

Integral is also a numeric typeclass. Num includes all numbers, including real numbers and integral numbers, Integral includes only integral (whole) numbers. In this typeclass are Int and Integer.

Floating includes only floating point numbers, so Float and Double.

A very useful function for dealing with numbers is fromIntegral. It has a type declaration of fromIntegral :: (Num b, Integral a) => a -> b. From its type signature we see that it takes an integral number and turns it into a more general number. That’s useful when you want integral and floating point types to work together nicely. For instance, the length function has a type declaration of length :: [a] -> Int instead of having a more general type of (Num b) => length :: [a] -> b. I think that’s there for historical reasons or something, although in my opinion, it’s pretty stupid. Anyway, if we try to get a length of a list and then add it to 3.2, we’ll get an error because we tried to add together an Int and a floating point number. So to get around this, we do fromIntegral (length [1,2,3,4]) + 3.2 and it all works out.

Notice that fromIntegral has several class constraints in its type signature. That’s completely valid and as you can see, the class constraints are separated by commas inside the parentheses.

🤩

Haskell – 59 – tipi e classi di tipi – 1

Continuo da qui, copio qui.

Previously we mentioned that Haskell has a static type system. The type of every expression is known at compile time, which leads to safer code. If you write a program where you try to divide a boolean type with some number, it won’t even compile. That’s good because it’s better to catch such errors at compile time instead of having your program crash. Everything in Haskell has a type, so the compiler can reason quite a lot about your program before compiling it.

Unlike Java or Pascal, Haskell has type inference. If we write a number, we don’t have to tell Haskell it’s a number. It can infer that on its own, so we don’t have to explicitly write out the types of our functions and expressions to get things done. We covered some of the basics of Haskell with only a very superficial glance at types. However, understanding the type system is a very important part of learning Haskell.

A type is a kind of label that every expression has. It tells us in which category of things that expression fits. The expression True is a boolean, "hello" is a string, etc.

Now we’ll use GHCI to examine the types of some expressions. We’ll do that by using the :t command which, followed by any valid expression, tells us its type. Let’s give it a whirl.

Prelude> :t 'a'
'a' :: Char
Prelude> :t True
True :: Bool
Prelude> :t "HELLO!"
"HELLO!" :: [Char]
Prelude> :t (True, 'a')
(True, 'a') :: (Bool, Char)
Prelude> :t 4 == 5
4 == 5 :: Bool

Here we see that doing :t on an expression prints out the expression followed by :: and its type. :: is read as “has type of“. Explicit types are always denoted with the first letter in capital case. 'a', as it would seem, has a type of Char. It’s not hard to conclude that it stands for character. True is of a Bool type. That makes sense. But what’s this? Examining the type of "HELLO!" yields a [Char]. The square brackets denote a list. So we read that as it being a list of characters. Unlike lists, each tuple length has its own type. So the expression of (True, 'a') has a type of (Bool, Char), whereas an expression such as ('a','b','c') would have the type of (Char, Char, Char). 4 == 5 will always return False, so its type is Bool.

Functions also have types. When writing our own functions, we can choose to give them an explicit type declaration. This is generally considered to be good practice except when writing very short functions. From here on, we’ll give all the functions that we make type declarations. Remember the list comprehension we made previously that filters a string so that only caps remain? Here’s how it looks like with a type declaration.

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

removeNonUppercase has a type of [Char] -> [Char], meaning that it maps from a string to a string. That’s because it takes one string as a parameter and returns another as a result. The [Char] type is synonymous with String so it’s clearer if we write removeNonUppercase :: String -> String. We didn’t have to give this function a type declaration because the compiler can infer by itself that it’s a function from a string to a string but we did anyway. But how do we write out the type of a function that takes several parameters? Here’s a simple function that takes three integers and adds them together:

addThree :: Int -> Int -> Int -> Int
addThree x y z = x + y + z

The parameters are separated with -> and there’s no special distinction between the parameters and the return type. The return type is the last item in the declaration and the parameters are the first three. Later on we’ll see why they’re all just separated with -> instead of having some more explicit distinction between the return types and the parameters like Int, Int, Int -> Int or something.

If you want to give your function a type declaration but are unsure as to what it should be, you can always just write the function without it and then check it with :t. Functions are expressions too, so :t works on them without a problem.

Nota: prima di continuare voglio aggiungere una cosa che Miran non dice (almeno non qui). Nella REPL di GHC non è possibile dichuarare i tipi, almeno non direttamente. Ma è semplicissimo (e spesso consigliabile) ricorrere a scrivere il codice in un file e caricarlo con :l (o :load). Per esempio la sunfiona addThree la metto nel file add3.hs:

add3.hs

addThree :: Int -> Int -> Int -> Int
addThree x y z = x + y + z
Prelude> :l add3
[1 of 1] Compiling Main             ( add3.hs, interpreted )
Ok, modules loaded: Main.
*Main> addThree 1 2 3
6
*Main> addThree 11 12 13
36

Here’s an overview of some common types.

Int stands for integer. It’s used for whole numbers. 7 can be an Int but 7.2 cannot. Int is bounded, which means that it has a minimum and a maximum value. Usually on 32-bit machines the maximum possible Int is 2147483647, (2^31 – 1) and the minimum is -2147483648.

Integer stands for, er … also integer. The main difference is that it’s not bounded so it can be used to represent really really big numbers. I mean like really big. Int, however, is more efficient.

fact.hs

factorial :: Integer -> Integer
factorial n = product [1..n]
*Main> :l fact
[1 of 1] Compiling Main             ( fact.hs, interpreted )
Ok, modules loaded: Main.
*Main> factorial 50
30414093201713378043612608166064768844377641568960512000000000000

Float is a real floating point with single precision.

circ-sp.hs

circumference :: Float -> Float
circumference r = 2 * pi * r
*Main> :l circ-sp
[1 of 1] Compiling Main             ( circ-sp.hs, interpreted )
Ok, modules loaded: Main.
*Main> circumference 4.0
25.132742
*Main> circumference 4
25.132742

Double is a real floating point with double the precision.

circ.dp.hs

circumference' :: Double -> Double
circumference' r = 2 * pi * r
*Main> :l circ-dp
[1 of 1] Compiling Main             ( circ-dp.hs, interpreted )
Ok, modules loaded: Main.
*Main> circumference' 4.0
25.132741228718345
*Main> circumference' 4
25.132741228718345

Bool is a boolean type. It can have only two values: True and False.

Char represents a character. It’s denoted by single quotes. A list of characters is a string.

Tuples are types but they are dependent on their length as well as the types of their components, so there is theoretically an infinite number of tuple types, which is too many to cover in this tutorial. Note that the empty tuple () is also a type which can only have a single value: ().

🤩

Haskell – 58 – inizio – 6

Continuo da qui, copio qui.

Tuple
In some ways, tuples are like lists — they are a way to store several values into a single value. However, there are a few fundamental differences. A list of numbers is a list of numbers. That’s its type and it doesn’t matter if it has only one number in it or an infinite amount of numbers. Tuples, however, are used when you know exactly how many values you want to combine and its type depends on how many components it has and the types of the components. They are denoted with parentheses and their components are separated by commas.

Another key difference is that they don’t have to be homogenous. Unlike a list, a tuple can contain a combination of several types.

Think about how we’d represent a two-dimensional vector in Haskell. One way would be to use a list. That would kind of work. So what if we wanted to put a couple of vectors in a list to represent points of a shape on a two-dimensional plane? We could do something like [[1,2],[8,11],[4,5]]. The problem with that method is that we could also do stuff like [[1,2],[8,11,5],[4,5]], which Haskell has no problem with since it’s still a list of lists with numbers but it kind of doesn’t make sense. But a tuple of size two (also called a pair) is its own type, which means that a list can’t have a couple of pairs in it and then a triple (a tuple of size three), so let’s use that instead. Instead of surrounding the vectors with square brackets, we use parentheses: [(1,2),(8,11),(4,5)]. What if we tried to make a shape like [(1,2),(8,11,5),(4,5)]? Well, we’d get this error:

Prelude> t = [[1,2],[8,11],[4,5]]
Prelude> t' = [[1,2],[8,11,5],[4,5]]
Prelude> u = [(1,2),(8,11),(4,5)]
Prelude> u' = [(1,2),(8,11,5),(4,5)]

:4:13: error:
    • Couldn't match expected type ‘(t1, t)’
                  with actual type ‘(Integer, Integer, Integer)’
    • In the expression: (8, 11, 5)
      In the expression: [(1, 2), (8, 11, 5), (4, 5)]
      In an equation for ‘u'’: u' = [(1, 2), (8, 11, 5), (4, 5)]
    • Relevant bindings include
        u' :: [(t1, t)] (bound at :4:1)

It’s telling us that we tried to use a pair and a triple in the same list, which is not supposed to happen. You also couldn’t make a list like [(1,2),("One",2)] because the first element of the list is a pair of numbers and the second element is a pair consisting of a string and a number. Tuples can also be used to represent a wide variety of data. For instance, if we wanted to represent someone’s name and age in Haskell, we could use a triple: ("Christopher", "Walken", 55). As seen in this example, tuples can also contain lists.

Use tuples when you know in advance how many components some piece of data should have. Tuples are much more rigid because each different size of tuple is its own type, so you can’t write a general function to append an element to a tuple — you’d have to write a function for appending to a pair, one function for appending to a triple, one function for appending to a 4-tuple, etc.

While there are singleton lists, there’s no such thing as a singleton tuple. It doesn’t really make much sense when you think about it. A singleton tuple would just be the value it contains and as such would have no benefit to us.

Like lists, tuples can be compared with each other if their components can be compared. Only you can’t compare two tuples of different sizes, whereas you can compare two lists of different sizes. Two useful functions that operate on pairs:

fst takes a pair and returns its first component.

Prelude> fst (8,11)
8
Prelude> fst ("Wow", False)
"Wow"

snd takes a pair and returns its second component. Surprise!

Prelude> snd (8,11)
11
Prelude> snd ("Wow", False)
False

Note: these functions operate only on pairs. They won’t work on triples, 4-tuples, 5-tuples, etc. We’ll go over extracting data from tuples in different ways a bit later.

A cool function that produces a list of pairs: zip. It takes two lists and then zips them together into one list by joining the matching elements into pairs. It’s a really simple function but it has loads of uses. It’s especially useful for when you want to combine two lists in a way or traverse two lists simultaneously. Here’s a demonstration.

Prelude>  zip [1,2,3,4,5] [5,5,5,5,5]
[(1,5),(2,5),(3,5),(4,5),(5,5)]
Prelude> zip [1 .. 5] ["one", "two", "three", "four", "five"]
[(1,"one"),(2,"two"),(3,"three"),(4,"four"),(5,"five")]

It pairs up the elements and produces a new list. The first element goes with the first, the second with the second, etc. Notice that because pairs can have different types in them, zip can take two lists that contain different types and zip them up. What happens if the lengths of the lists don’t match?

Prelude> zip [5,3,2,6,2,7,2,5,4,6,6] ["im","a","turtle"]
[(5,"im"),(3,"a"),(2,"turtle")]

The longer list simply gets cut off to match the length of the shorter one. Because Haskell is lazy, we can zip finite lists with infinite lists:

Prelude> zip [1..] ["apple", "orange", "cherry", "mango"]
[(1,"apple"),(2,"orange"),(3,"cherry"),(4,"mango")]

Here’s a problem that combines tuples and list comprehensions: which right triangle that has integers for all sides and all sides equal to or smaller than 10 has a perimeter of 24? First, let’s try generating all triangles with sides equal to or smaller than 10:

Prelude> triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]

We’re just drawing from three lists and our output function is combining them into a triple. If you evaluate that by typing out triangles in GHCI, you’ll get a list of all possible triangles with sides under or equal to 10. Next, we’ll add a condition that they all have to be right triangles. We’ll also modify this function by taking into consideration that side b isn’t larger than the hypothenuse and that side a isn’t larger than side b.

Prelude> rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]

We’re almost done. Now, we just modify the function by saying that we want the ones where the perimeter is 24.

Prelude> rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24] 
Prelude> rightTriangles'
[(6,8,10)]

And there’s our answer! This is a common pattern in functional programming. You take a starting set of solutions and then you apply transformations to those solutions and filter them until you get the right ones.

🤩

Haskell – 57 – inizio – 5

Continuo da qui, copio qui.

List comprehesion
If you’ve ever taken a course in mathematics, you’ve probably run into set comprehensions. They’re normally used for building more specific sets out of general sets. A basic comprehension for a set that contains the first ten even natural numbers is

The part before the pipe is called the output function, x is the variable, N is the input set and x <= 10 is the predicate. That means that the set contains the doubles of all natural numbers that satisfy the predicate.

If we wanted to write that in Haskell, we could do something like take 10 [2,4..]. But what if we didn’t want doubles of the first 10 natural numbers but some kind of more complex function applied on them? We could use a list comprehension for that. List comprehensions are very similar to set comprehensions. We’ll stick to getting the first 10 even numbers for now. The list comprehension we could use is [x*2 | x <- [1..10]]. x is drawn from [1..10] and for every element in [1..10] (which we have bound to x), we get that element, only doubled. Here’s that comprehension in action.

Prelude> [x*2 | x <- [1..10]]
[2,4,6,8,10,12,14,16,18,20]

As you can see, we get the desired results. Now let’s add a condition (or a predicate) to that comprehension. Predicates go after the binding parts and are separated from them by a comma. Let’s say we want only the elements which, doubled, are greater than or equal to 12.

Prelude> [x*2 | x <- [1..10], x*2 >= 12]
[12,14,16,18,20]

Cool, it works. How about if we wanted all numbers from 50 to 100 whose remainder when divided with the number 7 is 3? Easy.

Prelude> [ x | x <- [50..100], x `mod` 7 == 3]
[52,59,66,73,80,87,94]

Naturalmente si può usare la notazione prefissa che a me piace di più:

Prelude> [ x | x <- [50..100], mod x 7 == 3]
[52,59,66,73,80,87,94]

Success! Note that weeding out lists by predicates is also called filtering. We took a list of numbers and we filtered them by the predicate. Now for another example. Let’s say we want a comprehension that replaces each odd number greater than 10 with “BANG!” and each odd number that’s less than 10 with “BOOM!”. If a number isn’t odd, we throw it out of our list. For convenience, we’ll put that comprehension inside a function so we can easily reuse it.

Prelude> boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]

The last part of the comprehension is the predicate. The function odd returns True on an odd number and False on an even one. The element is included in the list only if all the predicates evaluate to True.

Prelude> boomBangs [7..13]
["BOOM!","BOOM!","BANG!","BANG!"]

We can include several predicates. If we wanted all numbers from 10 to 20 that are not 13, 15 or 19, we’d do:

Prelude> [x | x <- [10..20], x /= 13, x /= 15, x /= 19]
[10,11,12,14,16,17,18,20]

Not only can we have multiple predicates in list comprehensions (an element must satisfy all the predicates to be included in the resulting list), we can also draw from several lists. When drawing from several lists, comprehensions produce all combinations of the given lists and then join them by the output function we supply. A list produced by a comprehension that draws from two lists of length 4 will have a length of 16, provided we don’t filter them. If we have two lists, [2,5,10] and [8,10,11] and we want to get the products of all the possible combinations between numbers in those lists, here’s what we’d do.

Prelude> [x*y | x <- [2,5,10], y <- [8,10,11]]
[16,20,22,40,50,55,80,100,110]

As expected, the length of the new list is 9. What if we wanted all possible products that are more than 50?

Prelude> [ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]
[55,80,100,110]

How about a list comprehension that combines a list of adjectives and a list of nouns … for epic hilarity.

Prelude> nouns = ["hobo","frog","pope"]
Prelude> adjectives = ["lazy","grouchy","scheming"]
Prelude> [adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
["lazy hobo","lazy frog","lazy pope","grouchy hobo","grouchy frog","grouchy pope",
"scheming hobo","scheming frog","scheming pope"]

I know! Let’s write our own version of length! We’ll call it length'.

Prelude> length' xs = sum [1 | _ <- xs]

_ means that we don’t care what we’ll draw from the list anyway so instead of writing a variable name that we’ll never use, we just write _. This function replaces every element of a list with 1 and then sums that up. This means that the resulting sum will be the length of our list.

Just a friendly reminder: because strings are lists, we can use list comprehensions to process and produce strings. Here’s a function that takes a string and removes everything except uppercase letters from it.

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

Testing it out:

Prelude> removeNonUppercase "Hahaha! Ahahaha!"
"HA"
Prelude> removeNonUppercase "IdontLIKEFROGS"
"ILIKEFROGS"

The predicate here does all the work. It says that the character will be included in the new list only if it’s an element of the list ['A'..'Z']. Nested list comprehensions are also possible if you’re operating on lists that contain lists. A list contains several lists of numbers. Let’s remove all odd numbers without flattening the list.

Prelude> xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]
Prelude> [ [ x | x <- xs, even x ] | xs <- xxs]
[[2,2,4],[2,4,6,8],[2,4,2,6,2,6]]

You can write list comprehensions across several lines. So if you’re not in GHCI, it’s better to split longer list comprehensions across multiple lines, especially if they’re nested.

🤩

Haskell – 56 – inizio – 4

Continuo da qui, copio qui.

Sequenze
Miran titola “Texas ranges”; Miran rockz! 👽

What if we want a list of all numbers between 1 and 20? Sure, we could just type them all out but obviously that’s not a solution for gentlemen who demand excellence from their programming languages. Instead, we’ll use ranges. Ranges are a way of making lists that are arithmetic sequences of elements that can be enumerated. Numbers can be enumerated. One, two, three, four, etc. Characters can also be enumerated. The alphabet is an enumeration of characters from A to Z. Names can’t be enumerated. What comes after “John”? I don’t know.

To make a list containing all the natural numbers from 1 to 20, you just write [1..20]. That is the equivalent of writing [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] and there’s no difference between writing one or the other except that writing out long enumeration sequences manually is stupid.

Prelude> [1..20]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Prelude> ['a'..'z']
"abcdefghijklmnopqrstuvwxyz"
Prelude> ['K'..'Z']
"KLMNOPQRSTUVWXYZ"

Ranges are cool because you can also specify a step. What if we want all even numbers between 1 and 20? Or every third number between 1 and 20?

Prelude> [2,4..20]
[2,4,6,8,10,12,14,16,18,20]
Prelude> [3,6..20]
[3,6,9,12,15,18]

It’s simply a matter of separating the first two elements with a comma and then specifying what the upper limit is. While pretty smart, ranges with steps aren’t as smart as some people expect them to be. You can’t do [1,2,4,8,16..100] and expect to get all the powers of 2. Firstly because you can only specify one step. And secondly because some sequences that aren’t arithmetic are ambiguous if given only by a few of their first terms.

To make a list with all the numbers from 20 to 1, you can’t just do [20..1], you have to do [20,19..1].

Watch out when using floating point numbers in ranges! Because they are not completely precise (by definition), their use in ranges can yield some pretty funky results.

Prelude> [0.1, 0.3 .. 1]
[0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999]

My advice is not to use them in list ranges.

You can also use ranges to make infinite lists by just not specifying an upper limit. Later we’ll go into more detail on infinite lists. For now, let’s examine how you would get the first 24 multiples of 13. Sure, you could do [13,26..24*13]. But there’s a better way: take 24 [13,26..]. Because Haskell is lazy, it won’t try to evaluate the infinite list immediately because it would never finish. It’ll wait to see what you want to get out of that infinite lists. And here it sees you just want the first 24 elements and it gladly obliges.

A handful of functions that produce infinite lists:

cycle takes a list and cycles it into an infinite list. If you just try to display the result, it will go on forever so you have to slice it off somewhere.

Prelude> take 10 (cycle [1,2,3])
[1,2,3,1,2,3,1,2,3,1]
Prelude> take 12 (cycle "LOL ")
"LOL LOL LOL "

repeat takes an element and produces an infinite list of just that element. It’s like cycling a list with only one element.

Prelude> take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]

Although it’s simpler to just use the replicate function if you want some number of the same element in a list.

Prelude> replicate 3 10
[10,10,10]

🤩

Haskell – 55 – inizio – 3

Continuo da qui, copio qui.

Introduzione alle liste
Much like shopping lists in the real world, lists in Haskell are very useful. It’s the most used data structure and it can be used in a multitude of different ways to model and solve a whole bunch of problems. Lists are so awesome. In this section we’ll look at the basics of lists, strings (which are lists) and list comprehensions.

In Haskell, lists are a homogenous data structure. It stores several elements of the same type. That means that we can have a list of integers or a list of characters but we can’t have a list that has a few integers and then a few characters. And now, a list!

Note: We can use the let keyword to define a name right in GHCI. Doing let a = 1 inside GHCI is the equivalent of writing a = 1 in a script and then loading it. Non sono sicuro che sia necessario, difatti provo a ometterlo.

Prelude> lostNumbers = [4,8,15,16,23,42]
Prelude> lostNumbers
[4,8,15,16,23,42]

As you can see, lists are denoted by square brackets and the values in the lists are separated by commas. If we tried a list like [1,2,'a',3,'b','c',4], Haskell would complain that characters (which are, by the way, denoted as a character between single quotes) are not numbers. Speaking of characters, strings are just lists of characters. "hello" is just syntactic sugar for ['h','e','l','l','o']. Because strings are lists, we can use list functions on them, which is really handy.

A common task is putting two lists together. This is done by using the ++ operator.

Prelude> [1,2,3,4] ++ [9,10,11,12]
[1,2,3,4,9,10,11,12]
Prelude> "hello" ++ " " ++ "world"
"hello world"
Prelude> ['w','o'] ++ ['o','t']
"woot"

Watch out when repeatedly using the ++ operator on long strings. When you put together two lists (even if you append a singleton list to a list, for instance: [1,2,3] ++ [4]), internally, Haskell has to walk through the whole list on the left side of ++. That’s not a problem when dealing with lists that aren’t too big. But putting something at the end of a list that’s fifty million entries long is going to take a while. However, putting something at the beginning of a list using the : operator (also called the cons operator) is instantaneous.

Prelude> 'A':" SMALL CAT"
"A SMALL CAT"
Prelude> 0:[1,2,3,4,5]
[0,1,2,3,4,5]

Notice how : takes a number and a list of numbers or a character and a list of characters, whereas ++ takes two lists. Even if you’re adding an element to the end of a list with ++, you have to surround it with square brackets so it becomes a list.

[1,2,3] is actually just syntactic sugar for 1:2:3:[]. [] is an empty list. If we prepend 3 to it, it becomes [3]. If we prepend 2 to that, it becomes [2,3], and so on.

Note: [], [[]] and [[],[],[]] are all different things. The first one is an empty list, the seconds one is a list that contains one empty list, the third one is a list that contains three empty lists.

If you want to get an element out of a list by index, use !!. The indices start at 0.

Prelude> "Steve Buscemi" !! 6
'B'
Prelude> [9.4,33.2,96.2,11.2,23.25] !! 1
33.2

But if you try to get the sixth element from a list that only has four elements, you’ll get an error so be careful!

Lists can also contain lists. They can also contain lists that contain lists that contain lists …

Prelude> b = [[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]
Prelude> b
[[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]
Prelude> b ++ [[1,1,1,1]]
[[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3],[1,1,1,1]]
Prelude> [6,6,6]:b
[[6,6,6],[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]
Prelude> b !! 2
[1,2,2,3,4]

The lists within a list can be of different lengths but they can’t be of different types. Just like you can’t have a list that has some characters and some numbers, you can’t have a list that has some lists of characters and some lists of numbers.

Lists can be compared if the stuff they contain can be compared. When using <, <=, > and >= to compare lists, they are compared in lexicographical order. First the heads are compared. If they are equal then the second elements are compared, etc.

Prelude> [3,2,1] > [2,1,0]
True
Prelude> [3,2,1] > [2,10,100]
True
Prelude> [3,4,2] > [3,4]
True
Prelude> [3,4,2] > [2,4]
True
Prelude> [3,4,2] == [3,4,2]
True

What else can you do with lists? Here are some basic functions that operate on lists.

head takes a list and returns its head. The head of a list is basically its first element.

Prelude> head [5,4,3,2,1]
5

tail takes a list and returns its tail. In other words, it chops off a list’s head.

Prelude> tail [5,4,3,2,1]
[4,3,2,1]

last takes a list and returns its last element.

Prelude> last [5,4,3,2,1]
1

init takes a list and returns everything except its last element.

Prelude> init [5,4,3,2,1]
[5,4,3,2]

If we think of a list as a monster, here’s what’s what.

But what happens if we try to get the head of an empty list?

Prelude> head []
*** Exception: Prelude.head: empty list

Oh my! It all blows up in our face! If there’s no monster, it doesn’t have a head. When using head, tail, last and init, be careful not to use them on empty lists. This error cannot be caught at compile time so it’s always good practice to take precautions against accidentally telling Haskell to give you some elements from an empty list.

length takes a list and returns its length, obviously.

Prelude> length [5,4,3,2,1]
5

null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs)

Prelude> null [1,2,3]
False
Prelude> null []
True

reverse reverses a list.

Prelude> reverse [5,4,3,2,1]
[1,2,3,4,5]

take takes number and a list. It extracts that many elements from the beginning of the list.

Prelude> take 3 [5,4,3,2,1]
[5,4,3]
Prelude> take 1 [3,9,3]
[3]
Prelude> take 5 [1,2]
[1,2]
Prelude> take 0 [6,6,6]
[]

See how if we try to take more elements than there are in the list, it just returns the list. If we try to take 0 elements, we get an empty list.

drop works in a similar way, only it drops the number of elements from the beginning of a list.

Prelude> drop 3 [8,4,2,1,5,6]
[1,5,6]
Prelude> drop 0 [1,2,3,4]
[1,2,3,4]
Prelude> drop 100 [1,2,3,4]
[]

maximum takes a list of stuff that can be put in some kind of order and returns the biggest element.
minimum returns the smallest.

Prelude> minimum [8,4,2,1,5,6]
1
Prelude> maximum [1,9,2,3,4]
9

sum takes a list of numbers and returns their sum.
product takes a list of numbers and returns their product.

Prelude> sum [5,2,1,6,3,2,5,7]
31
Prelude> product [6,2,1,2]
24
Prelude> product [1,2,5,6,7,9,2,0]
0

elem takes a thing and a list of things and tells us if that thing is an element of the list. It’s usually called as an infix function because it’s easier to read that way.

Prelude> 4 `elem` [3,4,5,6]
True
Prelude> 10 `elem` [3,4,5,6]
False

ovviamente si può fare à la lisp

Prelude> elem 4 [3,4,5,6]
True
Prelude> elem 10 [3,4,5,6]
False

Those were a few basic functions that operate on lists. We’ll take a look at more list functions later.

🤩

Haskell – 54 – inizio – 2

Continuo da qui, copio qui.

Siamo sempre ai ripassi, ecco le prime funzioni 😋

In the previous section we got a basic feel for calling functions. Now let’s try making our own! Open up your favorite text editor and punch in this function that takes a number and multiplies it by two.

Prelude> doubleMe x = x + x
Prelude> doubleMe 21
42

Ma attenzione: il prof  stressa  di fare le cose per bene –concordo 😁

Functions are defined in a similar way that they are called. The function name is followed by parameters seperated by spaces. But when defining functions, there’s a = and after that we define what the function does. Save this as baby.hs or something. Now navigate to where it’s saved and run ghci from there. Once inside GHCI, do :l baby. Now that our script is loaded, we can play with the function that we defined.

Prelude> :l baby
[1 of 1] Compiling Main             ( baby.hs, interpreted )
Ok, modules loaded: Main.
*Main> doubleMe 10
20
*Main> doubleMe 2.7818
5.5636

Because + works on integers as well as on floating-point numbers (anything that can be considered a number, really), our function also works on any number. Let’s make a function that takes two numbers and multiplies each by two and then adds them together.

doubleUs x y = x*2 + y*2

Simple. We could have also defined it as doubleUs x y = x + x + y + y. Testing it out produces pretty predictable results (remember to append this function to the baby.hs file, save it and then do :l baby inside GHCI).

*Main> :l baby
[1 of 1] Compiling Main             ( baby.hs, interpreted )
Ok, modules loaded: Main.
*Main> doubleUs 4 9
26
*Main> doubleUs 2.3 34.2
73.0
*Main> doubleUs 28 88 + doubleMe 123
478

As expected, you can call your own functions from other functions that you made. With that in mind, we could redefine doubleUs like this:

doubleUs x y = doubleMe x + doubleMe y

This is a very simple example of a common pattern you will see throughout Haskell. Making basic functions that are obviously correct and then combining them into more complex functions. This way you also avoid repetition. What if some mathematicians figured out that 2 is actually 3 and you had to change your program? You could just redefine doubleMe to be x + x + x and since doubleUs calls doubleMe, it would automatically work in this strange new world where 2 is 3.

Functions in Haskell don’t have to be in any particular order, so it doesn’t matter if you define doubleMe first and then doubleUs or if you do it the other way around.

Now we’re going to make a function that multiplies a number by 2 but only if that number is smaller than or equal to 100 because numbers bigger than 100 are big enough as it is!

doubleSmallNumber x = if x > 100
                        then x
                        else x*2

Right here we introduced Haskell’s if statement. You’re probably familiar with if statements from other languages. The difference between Haskell’s if statement and if statements in imperative languages is that the else part is mandatory in Haskell. [Ricorda qualcuno? Sì ma zitto!]. In imperative languages you can just skip a couple of steps if the condition isn’t satisfied but in Haskell every expression and function must return something. We could have also written that if statement in one line but I find this way more readable. Another thing about the if statement in Haskell is that it is an expression. An expression is basically a piece of code that returns a value. 5 is an expression because it returns 5, 4 + 8 is an expression, x + y is an expression because it returns the sum of x and y. Because the else is mandatory, an if statement will always return something and that’s why it’s an expression. If we wanted to add one to every number that’s produced in our previous function, we could have written its body like this.

doubleSmallNumber' x = (if x > 100 then x else x*2) + 1

Had we omitted the parentheses, it would have added one only if x wasn’t greater than 100. Note the ' at the end of the function name. That apostrophe doesn’t have any special meaning in Haskell’s syntax. It’s a valid character to use in a function name. We usually use ' to either denote a strict version of a function (one that isn’t lazy) or a slightly modified version of a function or a variable. Because ' is a valid character in functions, we can make a function like this.

conanO'Brien = "It's a-me, Conan O'Brien!"

There are two noteworthy things here. The first is that in the function name we didn’t capitalize Conan’s name. That’s because functions can’t begin with uppercase letters. We’ll see why a bit later. The second thing is that this function doesn’t take any parameters. When a function doesn’t take any parameters, we usually say it’s a definition (or a name). Because we can’t change what names (and functions) mean once we’ve defined them, conanO'Brien and the string "It's a-me, Conan O'Brien!" can be used interchangeably.

OK, è solo un ripasso –ma fatto molto bene. Adesso pausa 😋

🤩

Haskell – 53 – inizio – 1

Continuo da qui, copio qui.

Ready, set, go!
Alright, let’s get started! Miran raccomanda qualche raccomandazione e dice che si userà ghci; OK. Ah! cambia il prompt: The prompt here is Prelude> but because it can get longer when you load stuff into the session, we’re going to use ghci>. If you want to have the same prompt, just type in :set prompt "ghci> ".

* hs-53 $ ghci
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
Prelude> :set prompt "ghci> "
ghci> 5 * 8 + 2
42
ghci>

Segue con cose  tranquille  note; p.es. non funziona 5 * -3 ma si deve scrivere 5 * (-3).

Boolean algebra is also pretty straightforward. As you probably know, && means a boolean and, || means a boolean or. not negates a True or a False.

OK, e poi… What about doing 5 + "llama" or 5 == True? Well, if we try the first snippet, we get a big scary error message!

ghci> 5 + "llama"

:3:1: error:
    • No instance for (Num [Char]) arising from a use of ‘+’
    • In the expression: 5 + "llama"
      In an equation for ‘it’: it = 5 + "llama"
ghci> 5 == True

:4:1: error:
    • No instance for (Num Bool) arising from the literal ‘5’
    • In the first argument of ‘(==)’, namely ‘5’
      In the expression: 5 == True
      In an equation for ‘it’: it = 5 == True
ghci>

OK, cose note. You can’t compare apples and oranges. We’ll take a closer look at types a bit later. Note: you can do 5 + 4.0 because 5 is sneaky and can act like an integer or a floating-point number. 4.0 can’t act like an integer, so 5 is the one that has to adapt.

ghci> 5 + 4.0
9.0
ghci>

You may not have known it but we’ve been using functions now all along. For instance, * is a function that takes two numbers and multiplies them. As you’ve seen, we call it by sandwiching it between them. This is what we call an infix function. Most functions that aren’t used with numbers are prefix functions. Let’s take a look at them.

ghci> succ 7
8

The succ function takes anything that has a defined successor and returns that successor. As you can see, we just separate the function name from the parameter with a space. Calling a function with several parameters is also simple. The functions min and max take two things that can be put in an order (like numbers!). min returns the one that’s lesser and max returns the one that’s greater. See for yourself:

ghci> min 9 10
9
ghci> min 3.4 1.2
1.2
ghci> max 100 101
101

However, if we wanted to get the successor of the product of numbers 9 and 10, we couldn’t write succ 9 * 10 because that would get the successor of 9, which would then be multiplied by 10. So 100. We’d have to write succ (9 * 10) to get 91.

ghci> succ (9 * 10)
91

If a function takes two parameters, we can also call it as an infix function by surrounding it with backticks. For instance, the div function takes two integers and does integral division between them. Doing div 92 10 results in a 9. But when we call it like that, there may be some confusion as to which number is doing the division and which one is being divided. So we can call it as an infix function by doing 92 `div` 10 and suddenly it’s much clearer.

ghci> div 92 10
9
ghci> 92 `div` 10
9

Lots of people who come from imperative languages tend to stick to the notion that parentheses should denote function application. For example, in C, you use parentheses to call functions like foo(), bar(1) or baz(3, "haha"). Like we said, spaces are used for function application in Haskell. So those functions in Haskell would be foo, bar 1 and baz 3 "haha". So if you see something like bar (bar 3), it doesn’t mean that bar is called with bar and 3 as parameters. It means that we first call the function bar with 3 as the parameter to get some number and then we call bar again with that number. In C, that would be something like bar(bar(3)).

OK, cose note ma riparto per bene, con un ripasso 😋

🤩