nntpnews.net

Global Usenet Archiver


Register

[Haskell-beginners] Just clarifying the "pred" and "succ" functionsin Haskell

Reply

  #1  
Old 06-02-10, 05:27 AM
Andy Elvey
 
Posts: n/a
Default [Haskell-beginners] Just clarifying the "pred" and "succ" functionsin Haskell

Hi all -

I'm doing a public-domain package of "functional" programs" in C, and
that involves trying to replicate (in C) a number of Haskell functions.

The two that I'm looking at now are "pred" and "succ". I've seen the
examples on zvon.org, which give "pred 5" as being 4, and "pred B" as
being "A". Ok, that's fine, no problem.

However, is my understanding correct that this can be extended to lists
(arrays in C) so that (for example) for a list ["foo", "bar", "baz"]
, "pred "bar" " would give you "foo" , and "succ "bar" " would give
you "baz"?

Thanks in advance - bye for now -
- Andy
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #2  
Old 06-02-10, 05:55 AM
Brandon S. Allbery KF8NH
 
Posts: n/a
Default Re: [Haskell-beginners] Just clarifying the "pred" and "succ"functions in Haskell

On Feb 6, 2010, at 00:27 , Andy Elvey wrote:
> However, is my understanding correct that this can be extended to
> lists (arrays in C) so that (for example) for a list ["foo", "bar",
> "baz"] , "pred "bar" " would give you "foo" , and "succ "bar" "
> would give you "baz"?



No. Leaving aside that you don't manipulate lists that way in
Haskell, "bar" is a random value of type String (which is [Char]), not
a member of an enumeration. For comparison:

> data MyType = Foo | Bar | Baz deriving Enum;
> -- pred Bar = Foo, succ Bar = Baz


Some languages (e.g. Perl) do give an enumerable value to Strings, but
`succ "Bar"' would be something like "Baq". (This could be done in
Haskell, with some pain; it starts with `instance (Enum a, Bounded a)
=> Enum [a] where...'.) You can't go from a string like "Bar" to
whatever lists might contain that string (and what if multiple lists
contained it?), so there's no way to get an interpretation like that;
you would need an enumerator which had access both to the list and the
member in question, whereas Enum has access only to the type. (There
exist dependent type systems where you could encode that information
into a defined (sub)type, but Haskell doesn't support it directly.)

What you *can* do is that, because the types of list and array indexes
are members of Enum, you can for example use Data.List.index to
determine the index (if any!) of that item in your list, then take
prev or succ of that. Beware of running off the end of the list,
though. (It's also more complicated for arrays because array indexes
are themselves defined by a typeclass `Ix'.)

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell]
Code:
Content visible to registered users only.
system administrator [openafs,heimdal,too many hats]
Code:
Content visible to registered users only.
electrical and computer engineering, carnegie mellon university KF8NH



_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iEYEARECAAYFAkttBC8ACgkQIn7hlCsL25UwJwCfRzQJ+uZrV7 KA4gPbUkghLbGR
ds8Anjt4VmJmKcso+2eBde4tQLkpTBi0
=lOwV
-----END PGP SIGNATURE-----

Reply With Quote
  #3  
Old 06-02-10, 06:32 AM
Andy Elvey
 
Posts: n/a
Default Re: [Haskell-beginners] Just clarifying the "pred" and "succ"functions in Haskell

Brandon S. Allbery KF8NH wrote:
> On Feb 6, 2010, at 00:27 , Andy Elvey wrote:
>> However, is my understanding correct that this can be extended to
>> lists (arrays in C) so that (for example) for a list ["foo", "bar",
>> "baz"] , "pred "bar" " would give you "foo" , and "succ "bar" "
>> would give you "baz"?

>
>
> No. Leaving aside that you don't manipulate lists that way in
> Haskell, "bar" is a random value of type String (which is [Char]), not
> a member of an enumeration. For comparison:
>
> > data MyType = Foo | Bar | Baz deriving Enum;
> > -- pred Bar = Foo, succ Bar = Baz

>
> Some languages (e.g. Perl) do give an enumerable value to Strings, but
> `succ "Bar"' would be something like "Baq". (This could be done in
> Haskell, with some pain; it starts with `instance (Enum a, Bounded a)
> => Enum [a] where...'.) You can't go from a string like "Bar" to
> whatever lists might contain that string (and what if multiple lists
> contained it?), so there's no way to get an interpretation like that;
> you would need an enumerator which had access both to the list and the
> member in question, whereas Enum has access only to the type. (There
> exist dependent type systems where you could encode that information
> into a defined (sub)type, but Haskell doesn't support it directly.)
>
> What you *can* do is that, because the types of list and array indexes
> are members of Enum, you can for example use Data.List.index to
> determine the index (if any!) of that item in your list, then take
> prev or succ of that. Beware of running off the end of the list,
> though. (It's also more complicated for arrays because array indexes
> are themselves defined by a typeclass `Ix'.)

Hi Brandon - thanks very much for that!
Ok. Yes, that is exactly what I was after. I was indeed thinking that
"pred" and "succ" related to the "previous" and "next" elements in a
list, but I now see that that is not the case.

So, I may look at doing what I would call "lpred" and lsucc" - the
predecessor and successor of a list element.
I'm somewhat surprised that (from what I can tell) Haskell doesn't seem
to have those two functions for a list. I may be wrong....

Anyway - thanks again. Bye for now -
- Andy

_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-beginners] "reusable" data in Haskell John Velman fa.haskell 2 04-12-09 04:34 PM
["tl","ar","bn","gu","hi","kn","ml","mr","ne","pa","ta","te","ur"], http://meami.org sci.math 2 16-10-09 10:02 AM
[Haskell-beginners] Help with "20 intermediate haskell exercises" Patrick LeBoutillier fa.haskell 6 06-07-09 10:10 PM
[Haskell-beginners] "computation", "action" Michael P Mossey fa.haskell 3 20-05-09 12:27 AM
[Haskell-beginners] What is the Haskell idiom for "if then else ifthen" Peter Hickman fa.haskell 3 31-03-09 09:03 AM


All times are GMT +1. The time now is 04:55 AM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here

Abuse Ticket System