Talk:Haskell/Variables and functions
From Wikibooks, the open-content textbooks collection
Contents |
[edit] #haskell brainstorming notes 2006-07-08
DH: Firstly I think we should rip out that stuff about tail recursion. It's much to in-depth for that level.
DH: A better introduction wouldn't spend so much time talking about how to emulate other languages.
DH: I know newcomers struggle with the lack of loops -- I did -- but I don't think that's sufficient reason to dedicate the entire 'Basic concepts' chapter to overcoming it.
EYK: i think it would be fair to write this book as if the reader has never programmed before
DH: And I think we should have a whole chapter on types.
DH: I think the main faults of that page are:
- Bad focus on what Haskell's about
- Assumes too much of the reader (recursion, types).
A better introduction wouldn't spend so much time talking about how to emulate other languages.
DH: Perhaps we could change this into a brief chapter explaining the _very_ basics -- arithmetic and so forth, before outlining the rest of the book, or at least the rest of the first section of the book
EYK: a brief romp through the interpreter? try this! now see what happens what you try that?
[edit] Functions with functions
SH: I hope it wasn't out of place but in reading through this introductory chapter I decided to add an example of calling a functionwithin another function (assuming this was the intended meaning of the section. I'm by no means a haskell programmer so if something was amiss please change it and let me know.
- Most excellent! You read our minds exactly -- Kowey 09:44, 11 July 2006 (UTC)
-
- I've added an exercise for various-parameter functions and also an exercise for reusing a previously written function, both related. Is that ok ? -- Pupeno 16:23, 4 November 2006 (UTC)
-
-
- Thanks for that. Looks good to me. There might be some stuff from Haskell/YAHT we should steal as well -- Kowey 17:54, 4 November 2006 (UTC)
-
[edit] Bug in "area" exercise
The code doesn't work as stated... since "area 5" multiplies a real and integer. One could use "area 5.0" or amend the discussion to include this problem. (Since it was discussed above)
- Hi, could you please clarify that with an example? I have tested the code... but I could always be mistaken -- Kowey 16:40, 27 May 2007 (UTC)
- I am wondering if perhaps he just assumed it? I thought the same thing, wondering why, for example, pi * 5^2 worked. I did :t r and :t 5^2 to learn that one was an Integer and one a Num, which made me feel a little better. But it might be worth pointing out. Perhaps if I was bolder, like you're supposed to be with wiki, I would just do it, but I'm a total haskell newbie. -- Msouth 03:53, 15 July 2007 (UTC)
- I added a note. I am a newbie, so someone please clarify/correct. -- Msouth 04:30, 15 July 2007 (UTC)
- I am wondering if perhaps he just assumed it? I thought the same thing, wondering why, for example, pi * 5^2 worked. I did :t r and :t 5^2 to learn that one was an Integer and one a Num, which made me feel a little better. But it might be worth pointing out. Perhaps if I was bolder, like you're supposed to be with wiki, I would just do it, but I'm a total haskell newbie. -- Msouth 03:53, 15 July 2007 (UTC)
- Ack! Now I see the problem (thanks). It looks like I've accidentally stumbled across the dreaded Monomorhpism restriction (sort of your explanation). It means my initial explanation of (pi * r) isn't quite right. But then again, I don't think I want to get into that restriction right now because it's scary sounding. Will have to think about this -- Kowey 04:50, 15 July 2007 (UTC)
- (by the way, I've just posted an article about this on my blog) -- Kowey 06:26, 15 July 2007 (UTC)
-
- Hi Msouth, I have added an improved explanation in the Types section just above this "area" exercise. It has not been vetted by the Haskell community yet, but maybe you should have a look and see what you think -- Kowey 12:00, 19 July 2007 (UTC)
[edit] What does this sentence mean?
"Informally, the r in let r = 0 is true when you are in the top level of the interpreter". It's "true"? Like a true boolean? I don't understand this sentence, what it's telling me, what the top level of the interpreter is, etc.-- Msouth 04:37, 15 July 2007 (UTC)
- I have removed the offending text. I don't know what it means either. Definitely not true like true boolean, though. Thanks! -- Kowey 06:33, 15 July 2007 (UTC)
[edit] Solutions to problems
Where are the answers for these supposed to be? For example, I'm pretty sure "cylinder b h = (pi * (b ^ 2)) * h" is a valid answer, but how do I check?
- Yeah, that's an anonying part of this book; we haven't worked out all the solutions yet. I think it would be fair to put them all in a subpage; e.g. Haskell/Solutions/Variables and functions -- Kowey 07:29, 7 February 2007 (UTC)
[edit] Beginner Insight
This may be beginner's insight, or it may just be that I'm an idiot but:
Prelude> let pi = 3.14159265358979323846264338327950 Prelude> let area r = pi * r ^ 2 Prelude> area 5
...wait a sec- I thought we weren't allowed to multiply an integer by a double? The area function does though?
- Elin
- Thanks for that. There's a somewhat tricky technical explanation behind this that I had tried to make clearer (see the discussion above). Well, I've had another go at it (in the paragraph above). Hopefully it is a little better. The basic idea is that numbers like
5can actually be either integers, doubles or something else. Usually, the type is inferred from the context, but in some situations, for example, when you just say something likelet r = 5, there is no context. Haskell has to assign a type to it, so it defaults toInteger. However, inarea 5, we know thatareaexpects a double as input, so Haskell can infer that the5should be treated as a double. Hope that helps. -- Kowey 17:30, 28 July 2007 (UTC)
[edit] Truncation
"Don't worry about all those missing digits; they're just skipped when displaying the value. All the digits will be used in any future calculations."
Prelude> let pi = 3.14159265358979323846264338327950 Prelude> pi - 3.141592653589793238462643 0.0
كсηפ Cyp 20:00, 1 September 2007 (UTC)
Hum ... I get
Prelude> pi - 3.141592653589793 0.0 Prelude> pi - 3.14159265358979 3.1086244689504383e-15 Prelude> (pi - 3.141592653589793)*1000000000000000 0.0
The middle output showed me that a lot of digits were still there, so perhaps the first output was just being, y'know, truncated. So I tried to get these digits with the last output, but that failed. Then I noticed one other thing: most of the digits in the middle output are simply wrong!
Actually, since Haskell is a lazy language, I (a mathematician, not a computer programmer) don't understand why we need to bother with all this Float nonsense (where we pretend that a real number has only finitely many digits, and we just try to make sure that we have more digits than anybody will really need). Shouldn't a lazy language simply calculate the digits we need when we want them?
—Toby Bartels (talk) 18:08, 16 November 2008 (UTC)
- There are of course ways to do arbitrary precision calculations, but the
DoubleandFloattypes are the standard IEEE floating point numbers. Execution speed and all that. - -- apfeλmus 10:14, 17 November 2008 (UTC)
[edit] "let" syntax in Hugs?
If this command does not work, you are probably using hugs instead of GHCi, which expects a slightly different syntax.
I don't suppose anyone would like to tell readers just what the Hugs syntax is, would they? A quick search did not turn up anything. --75.15.138.149 15:39, 16 September 2007 (UTC)
Hugs can only evaluate expression, which means that you cannot bind variable across multiple prompt lines. Other than that, normal let expression work, of course.
> let x = (1+2) in x*x 9
But you can't use this x in the next line. I filed a feature request for Hugs. -- apfeλmus 06:55, 17 September 2007 (UTC)
[edit] variables that don't change
I know that programmers are used to variables for names that are assigned to numbers, strings, and other data structures. But if I understand correctly so far, Haskell variables are not allowed to vary, which makes for a rather odd use of the term. I think it was probably better off to stick with the term symbol.
- Well, the standard term is variable, we can't just change it to symbol I'm afraid.
- Also note that the mutable variables (imperative) programmers are used are much younger than the variables that have been used in mathematics for centuries and which work just like the Haskell ones. They still "vary" in the sense that they are a placeholder for a variety of values, it's just that they don't change once they are in scope.
- -- apfeλmus 08:57, 7 December 2008 (UTC)

