POV-Ray : Newsgroups : povray.off-topic : Try Haskell : Re: Try Haskell Server Time
4 Sep 2024 17:22:41 EDT (-0400)
  Re: Try Haskell  
From: Invisible
Date: 3 Mar 2010 11:30:18
Message: <4b8e8e9a$1@news.povray.org>
scott wrote:
>> Hmm, maybe I should write my own?
> 
> It's very useful for an absolute beginner, these are the things you need 
> to know that a lot of tutorials assume you somehow magically know already.

The hard thing about writing a tutorial is that by the time you're 
experienced enough to be an authority on the language, half the stuff 
that trips newbies up is so utterly "obvious" to you that you forget to 
even mention it. (Plus you're all excited and you want to jabber 
incoherently about the cool stuff, not the dry old "this is how you 
escape a newline character".)

>> > :t fst
>> Take a look at this type signature. Can you figure out what it means?
>>
>> > fst (5, 'x')
>> Did that do what you expected?
> 
> Well it seems to return the first item from a tuple.
> 
> Is it just a shorthand for "take 1"?
> 
> Ah no, it seems those list operators don't work with tuples :-(

It's all in the type signature.

[Int] means a list of integers. It can contain any possible number of 
integers (including zero or infinity). [Char] means a list of characters.

Tuples, on the other hand, have a different type for each size, and each 
combination of element types. For example, (5, 'x') is of type (Int, 
Char), but (5, 'x', False) is of type (Int, Char, Bool). As far as the 
type system is concerned, these two types are completely unrelated.

Because of this, you cannot write a function that works on any possible 
size of tuple. It's impossible. (You can write a *method* that works for 
any size of tuple that you've manually written a definition for, but 
that's about it.)

In truth, if you're using tuples bigger than about three or four 
elements, you really ought to define a custom type for this stuff 
instead of relying on tuples. Tuples are really for simple, quick things 
like returning multiple results from a function, or for key/value pairs. 
Stuff like that.

...and this whole explanation is a good example of the kind of thing 
that's hard to explain in an interactive tutorial. ;-)

> So this means "let x = 5 in (the following expression)..."

Indeed yes.

> Now Step 7 in the original tutorial makes sense :-)

Yeah. It's simple, it's just explained badly...

>> OK, I'm going to stop now...
> 
> OK I need to download the compiler if I am to actually learn any of this...

I can talk some more if you want?

But yes, this (alpha) web thingy lacks several important features:

- You can't define things and refer to them later. Each expression is 
processed independently.

- You can't write multi-line definitions.

- You can't do I/O. (And the error message if you try is cryptic to say 
the least!)

- You can't use all of the standard Haskell libraries, just the handful 
the interpretter offers.

In a real Haskell interpretter, you can usually do stuff like

 > let factorial n = product [1..n]
 > let choose n k = (factorial n) / (factorial k * factorial (n - k))
 > choose 3 5

In the online interpretter, you'd have to do

 > let factorial n = product [1..n]; choose n k = (factorial n) / 
(factorial k * factorial (n - k)) in choose 3 5

This, coupled with the horrifyingly buggy command history, is just no 
fun at all.

Are you actually interested enough to want to play with it more?


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.