![]() In most imperative languages functions are called by writing the function name and then writing its parameters in parentheses, usually separated by commas. Let's take a look at them.įunctions are usually prefix so from now on we won't explicitly state that a function is of the prefix form, we'll just assume it. Most functions that aren't used with numbers are prefix functions. As you've seen, we call it by sandwiching it between them. For instance, * is a function that takes two numbers and multiplies them. You may not have known it but we've been using functions now all along. 4.0 can't act like an integer, so 5 is the one that has to adapt. Note: you can do 5 + 4.0 because 5 is sneaky and can act like an integer or a floating-point number. We'll take a closer look at types a bit later. But the catch is that they both have to be the same type of thing. Whereas + works only on things that are considered numbers, = works on any two things that can be compared. If we tried to do True = 5, GHCI would tell us that the types don't match. + expects its left and right side to be numbers. Even if it wasn't "llama" but "four" or "4", Haskell still wouldn't consider it to be a number. Yikes! What GHCI is telling us here is that "llama" is not a number and so it doesn't know how to add it to 5. In the definition of `it': it = 5 + "llama" Possible fix: add an instance declaration for (Num ) What about doing 5 + "llama" or 5 = True? Well, if we try the first snippet, we get a big scary error message! As you probably know, & means a boolean and, || means a boolean or. Doing 5 * -3 will make GHCI yell at you but doing 5 * (-3) will work just fine.īoolean algebra is also pretty straightforward. If we want to have a negative number, it's always best to surround it with parentheses. A little pitfall to watch out for here is negating numbers. Pretty cool, huh? Yeah, I know it's not but bear with me. We can use parentheses to make the precedence explicit or to change it. We can also use several operators on one line and all the usual precedence rules are obeyed. ![]() If you want to have the same prompt, just type in :set prompt "ghci> ". done.Ĭongratulations, you're in GHCI! The prompt here is Prelude> but because it can get longer when you load stuff into the session, we're going to use ghci>. You will be greeted with something like this. The first thing we're going to do is run ghc's interactive mode and call some function to get a very basic feel for haskell. Alright, let's get started! If you're the sort of horrible person who doesn't read introductions to things and you skipped it, you might want to read the last section in the introduction anyway because it explains what you need to follow this tutorial and how we're going to load functions.
0 Comments
Leave a Reply. |