POV-Ray : Newsgroups : povray.off-topic : A Haskell question... Server Time
14 Oct 2026 23:17:20 EDT (-0400)
  A Haskell question... (Message 1 to 16 of 16)  
From: Darren New
Subject: A Haskell question...
Date: 2 Feb 2008 22:43:22
Message: <47a5385a$1@news.povray.org>
How would you write this in Haskell?

struct Point {int x; int y;}
distance = sqrt(p.x*p.x + p.y*p.y);

Oh, right, Andrew doesn't know any C-like languages. Hmmm...

A "point" structure with an X and a Y element, and a distance function 
that takes a point and calculates its distance from the origin.

Then, is it possible to add a Z element to the "Point" type without 
having to rewrite the "distance" part? (Sure, it'll give the wrong 
answer, but that's the point I'm trying to make with someone elsewhere 
claiming strong typing avoids this sort of error.)

-- 
   Darren New / San Diego, CA, USA (PST)
     On what day did God create the body thetans?


Post a reply to this message

From: Orchid XP v7
Subject: Re: A Haskell question...
Date: 3 Feb 2008 04:15:23
Message: <47a5862b$1@news.povray.org>
Darren New wrote:
> How would you write this in Haskell?
> 
> struct Point {int x; int y;}

   data Point = Point {x, y :: Int}

Caution: Note that the names "x" and "y" must be unique within the 
entire module. For this reason, you'd probably use longer names.

For something like this, a "typical" Haskell approach would "probably" 
use unnamed fields:

   data Point = Point Int Int

In either case, the following are all valid:

   let p = Point 3 6 ...
   my_function (Point x y) = ...

If the fields are named, you can also write

   let p = Point {x=3, y=6} ...
   let p = Point {y=6, x=3} ...
   my_function p = ... x p ...

The expression "x p" is like the Pascal-style "p.x". (That is, "x" is a 
function that takes a Point and yields its X coordinate.)

> distance = sqrt(p.x*p.x + p.y*p.y);

   distance (Point x y) = sqrt (x*x + y*y)

> Then, is it possible to add a Z element to the "Point" type without 
> having to rewrite the "distance" part? (Sure, it'll give the wrong 
> answer, but that's the point I'm trying to make with someone elsewhere 
> claiming strong typing avoids this sort of error.)

If you change the function to

   distance p = sqrt (x p * x p + y p * y p)

then it will work with either of

   data Point = Point {x, y :: Int}
   data Point = Point {x, y, z :: Int}

If you're asking about having two seperate Point types in the same 
program... that's another story.

Clear?

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: A Haskell question...
Date: 3 Feb 2008 10:00:20
Message: <47a5d704@news.povray.org>
Wouldn't povray.off-topic.fan.haskell.bork.bork.bork be a more 
appropriate group? :)


Post a reply to this message

From: nemesis
Subject: Re: A Haskell question...
Date: 3 Feb 2008 11:00:00
Message: <web.47a5e428f795a8d6ce60c5620@news.povray.org>
Nicolas Alvarez <nic### [at] gmailisthebestcom> wrote:
> Wouldn't povray.off-topic.fan.haskell.bork.bork.bork be a more
> appropriate group? :)

huhuhuhuhu...


Post a reply to this message

From: Warp
Subject: Re: A Haskell question...
Date: 4 Feb 2008 04:54:50
Message: <47a6e0ea@news.povray.org>
Orchid XP v7 <voi### [at] devnull> wrote:
>    data Point = Point {x, y :: Int}

> Caution: Note that the names "x" and "y" must be unique within the 
> entire module. For this reason, you'd probably use longer names.

  Doesn't Haskell support encapsulation?

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: A Haskell question...
Date: 4 Feb 2008 04:56:00
Message: <47a6e130$1@news.povray.org>
Warp wrote:
> Orchid XP v7 <voi### [at] devnull> wrote:
>>    data Point = Point {x, y :: Int}
> 
>> Caution: Note that the names "x" and "y" must be unique within the 
>> entire module. For this reason, you'd probably use longer names.
> 
>   Doesn't Haskell support encapsulation?

Define "encapsulation".

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Invisible
Subject: Re: A Haskell question...
Date: 4 Feb 2008 10:11:27
Message: <47a72b1f$1@news.povray.org>
Warp wrote:

>   Doesn't Haskell support encapsulation?

Last time I checked, Java has the same limitation.

In a single Java class, all methods must have unique names. (Ignoring 
for a moment the fact that Java supports overloading which Haskell 
doesn't.) Similarly, in a Haskell module all function names must be unique.

Either way, you can't use the same name for multiple things in a single 
source code file, in either language. And yet I don't see people 
claiming that Java doesn't "support encapsulation". So I'm not sure what 
you're getting at...

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: A Haskell question...
Date: 4 Feb 2008 10:16:03
Message: <47a72c33@news.povray.org>
Invisible escribió:
> Warp wrote:
> 
>>   Doesn't Haskell support encapsulation?
> 
> Last time I checked, Java has the same limitation.
> 
> In a single Java class, all methods must have unique names. (Ignoring 
> for a moment the fact that Java supports overloading which Haskell 
> doesn't.) Similarly, in a Haskell module all function names must be unique.
> 
> Either way, you can't use the same name for multiple things in a single 
> source code file, in either language. And yet I don't see people 
> claiming that Java doesn't "support encapsulation". So I'm not sure what 
> you're getting at...
> 

Without knowing a single thing about haskell, "module" *sounds* to me 
like a Java package, not a class...


Post a reply to this message

From: Invisible
Subject: Re: A Haskell question...
Date: 4 Feb 2008 10:28:46
Message: <47a72f2e$1@news.povray.org>
Nicolas Alvarez wrote:

> Without knowing a single thing about haskell, "module" *sounds* to me 
> like a Java package, not a class...

Each Java class has to go into a separate source code file. Each Haskell 
module has to go into a seperate source code file. Both languages 
require unique names within a single source file file. ;-)

(And a Java "package" is really just a bunch of classes that have the 
same prefix in their name, nothing more...)

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: A Haskell question...
Date: 4 Feb 2008 14:05:49
Message: <47a7620c@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> Warp wrote:

> >   Doesn't Haskell support encapsulation?

> Last time I checked, Java has the same limitation.

  You said:

> > struct Point {int x; int y;}
> 
>    data Point = Point {x, y :: Int}
> 
> Caution: Note that the names "x" and "y" must be unique within the
> entire module. For this reason, you'd probably use longer names.

  I understand to mean that the names 'x' and 'y' in your code above
actually garbage the global namespace.

  In the original C code they are local to 'Point' and do not affect
anything outside it.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v7
Subject: Re: A Haskell question...
Date: 4 Feb 2008 16:47:24
Message: <47a787ec$1@news.povray.org>
>>>   Doesn't Haskell support encapsulation?
> 
>> Last time I checked, Java has the same limitation.
> 
>   You said:
> 
>>> struct Point {int x; int y;}
>>    data Point = Point {x, y :: Int}
>>
>> Caution: Note that the names "x" and "y" must be unique within the
>> entire module. For this reason, you'd probably use longer names.
> 
>   I understand to mean that the names 'x' and 'y' in your code above
> actually garbage the global namespace.

No - the namespace of the current module. And in Java it would be the 
same - the names are local to this class file.

>   In the original C code they are local to 'Point' and do not affect
> anything outside it.

That's true...

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: A Haskell question...
Date: 4 Feb 2008 16:50:11
Message: <47a78893@news.povray.org>
Orchid XP v7 escribió:
> No - the namespace of the current module. And in Java it would be the 
> same - the names are local to this class file.

No. In Java it's local to the class, not to the file. You can have 
multiple classes in a single source file, as long as only one of them is 
public.


Post a reply to this message

From: Orchid XP v7
Subject: Re: A Haskell question...
Date: 4 Feb 2008 16:53:35
Message: <47a7895f$1@news.povray.org>
Nicolas Alvarez wrote:
> Orchid XP v7 escribió:
>> No - the namespace of the current module. And in Java it would be the 
>> same - the names are local to this class file.
> 
> No. In Java it's local to the class, not to the file. You can have 
> multiple classes in a single source file, as long as only one of them is 
> public.

Really? Interesting... I wasn't aware Java had non-public classes. 
(Although I did wonder why the hell you have to write "public" at the 
start of every class.)

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: A Haskell question...
Date: 4 Feb 2008 17:06:01
Message: <47a78c49$1@news.povray.org>
Orchid XP v7 escribió:
> Really? Interesting... I wasn't aware Java had non-public classes. 
> (Although I did wonder why the hell you have to write "public" at the 
> start of every class.)

If you don't add 'public', the class is only accessible from other 
classes on the same package.


Post a reply to this message

From: Orchid XP v7
Subject: Re: A Haskell question...
Date: 4 Feb 2008 17:08:21
Message: <47a78cd5$1@news.povray.org>
Nicolas Alvarez wrote:
> Orchid XP v7 escribió:
>> Really? Interesting... I wasn't aware Java had non-public classes. 
>> (Although I did wonder why the hell you have to write "public" at the 
>> start of every class.)
> 
> If you don't add 'public', the class is only accessible from other 
> classes on the same package.

Well, I didn't know that.

Interesting - once or twice a year that might be useful...

So much for university education, eh? Mind you, we learned Java from the 
guy who tried to tell us that the maximum range of a "double" is "more 
than the number of atoms in the universe". ;-)

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Michael Zier
Subject: Re: A Haskell question...
Date: 5 Feb 2008 01:59:18
Message: <47a80946$1@news.povray.org>
Am Mon, 04 Feb 2008 15:28:46 +0000 schrieb Invisible:

> 
> Each Java class has to go into a separate source code file. Each Haskell

Oh, then I have to throw away my Java compiler, because I usually write 
all the classes I need for my (admittedly small) mobile phone projects 
into one file. And it compiles! Bad compiler! 

Ok, it's not good for reusability, I know that...


Post a reply to this message

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