Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  minimal Haskell concepts subset (Dani?l de Kok)
   2. Re:  minimal Haskell concepts subset (Davi Santos)
   3.  Haskell-style types in C or C++ (Christopher Howard)
   4. Re:  Haskell-style types in C or C++ (Michael Xavier)
   5. Re:  Haskell-style types in C or C++ (Alexander Batischev)
   6.  Ambiguous type variable (Ovidiu Deac)
   7. Re:  Ambiguous type variable (David McBride)


----------------------------------------------------------------------

Message: 1
Date: Sat, 30 Jul 2011 14:01:41 +0200
From: Dani?l de Kok <m...@danieldk.eu>
Subject: Re: [Haskell-beginners] minimal Haskell concepts subset
To: Davi Santos <dps....@gmail.com>
Cc: beginners@haskell.org
Message-ID: <8460350a-dba0-41a0-8046-5e9e82e8d...@danieldk.eu>
Content-Type: text/plain; charset="iso-8859-1"

Hi Davi,

On Jul 25, 2011, at 11:17 PM, Davi Santos wrote:
> the first and best I found was http://learnyouahaskell.com/chapters.
> The point is that I spent too much time (almost a year) learning, and I am 
> not -productive- yet.

I think Learn You a Haskell is a good book. But if it is all too overwhelming I 
echo the recommendations for Graham Hutton's book - it is short and simple.

Other than that, I can only say: write, write, write. I lot of things are 
easier than they seem once you start using them. If you are a Unix user, a good 
way to start is to write small utilities you'd write in sh, Python, Perl, or 
Ruby in Haskell from now on. Start with stupid, obvious implementations, and 
refine them as you learn new abstractions and patterns.

This worked very well for me, some of my first Haskell programs were:

- Randomly pick n lines from a file.
- Normalize sets of scores.
- Filter lists of features in files.

Just basic things that I needed at work, and I'd normally write a Python script 
for.

Once you have written some code and read about an abstraction, you'll probably 
think "oh, this would improve program XYZ".

Good luck,
Dani?l

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110730/11344327/attachment-0001.htm>

------------------------------

Message: 2
Date: Sat, 30 Jul 2011 09:14:58 -0300
From: Davi Santos <dps....@gmail.com>
Subject: Re: [Haskell-beginners] minimal Haskell concepts subset
To: Dani?l de Kok <m...@danieldk.eu>
Cc: beginners@haskell.org
Message-ID:
        <CANWsST94+9Ovk4vs-F82fro6xbBZr_G=rmheftrqlcl-bz9...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Dani?l,
I was on that road already.
I made a relatively complex file parser and other things.

Unfortunately I will stop learning for a while, because my actual project
isn't adequate to use Haskell.
But I already can feel functional concepts being useful to apply in other
languages.
The time spent was worthy.
And I have no fear of novelties* anymore.   ; )

Davi

* -> even ~70-year old "novelties"  :)

On Sat, Jul 30, 2011 at 9:01 AM, Dani?l de Kok <m...@danieldk.eu> wrote:

> Hi Davi,
>
> On Jul 25, 2011, at 11:17 PM, Davi Santos wrote:
>
> the first and best I found was http://learnyouahaskell.com/chapters.
> The point is that I spent too much time (almost a year) learning, and I am
> not -productive- yet.
>
>
> I think Learn You a Haskell is a good book. But if it is all too
> overwhelming I echo the recommendations for Graham Hutton's book - it is
> short and simple.
>
> Other than that, I can only say: write, write, write. I lot of things are
> easier than they seem once you start using them. If you are a Unix user, a
> good way to start is to write small utilities you'd write in sh, Python,
> Perl, or Ruby in Haskell from now on. Start with stupid, obvious
> implementations, and refine them as you learn new abstractions and patterns.
>
> This worked very well for me, some of my first Haskell programs were:
>
> - Randomly pick n lines from a file.
> - Normalize sets of scores.
> - Filter lists of features in files.
>
> Just basic things that I needed at work, and I'd normally write a Python
> script for.
>
> Once you have written some code and read about an abstraction, you'll
> probably think "oh, this would improve program XYZ".
>
> Good luck,
> Dani?l
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110730/4c422c0b/attachment-0001.htm>

------------------------------

Message: 3
Date: Sat, 30 Jul 2011 10:45:06 -0800
From: Christopher Howard <christopher.how...@frigidcode.com>
Subject: [Haskell-beginners] Haskell-style types in C or C++
To: Haskell Beginners <beginners@haskell.org>
Message-ID: <4e345132.7050...@frigidcode.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

One of the things that I love about Haskell is the syntax for creating 
user defined types. E.g.:

Data QueryResult = NoResult | DatabaseError String | Results [String]

I can prototype an entire program around these self-made types and it is 
a lot of fun. Out of intellect curiosity, though: what would be the 
equivalent construction in C or C++? (Say, for the type defined above).

-- 
frigidcode.com
theologia.indicium.us



------------------------------

Message: 4
Date: Sat, 30 Jul 2011 12:51:13 -0700
From: Michael Xavier <nemesisdes...@gmail.com>
Subject: Re: [Haskell-beginners] Haskell-style types in C or C++
To: Christopher Howard <christopher.how...@frigidcode.com>
Cc: Haskell Beginners <beginners@haskell.org>
Message-ID:
        <CANk=zmGo_o=UgAxvR+UAW+O2qBiY3_BmGpxbaGq=zpfovhv...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

You should look into the Union datatype. It has been a long while since I
coded in C but I believe it allowed you to define a struct-like datatype
which could contain exactly 1 value from a list of datatypes. I found a
forum post about it:

http://www.go4expert.com/forums/showthread.php?t=15

Hope that helps.

On Sat, Jul 30, 2011 at 11:45 AM, Christopher Howard <
christopher.how...@frigidcode.com> wrote:

> One of the things that I love about Haskell is the syntax for creating user
> defined types. E.g.:
>
> Data QueryResult = NoResult | DatabaseError String | Results [String]
>
> I can prototype an entire program around these self-made types and it is a
> lot of fun. Out of intellect curiosity, though: what would be the equivalent
> construction in C or C++? (Say, for the type defined above).
>
> --
> frigidcode.com
> theologia.indicium.us
>
> ______________________________**_________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
>



-- 
Michael Xavier
http://www.michaelxavier.net
LinkedIn <http://www.linkedin.com/pub/michael-xavier/13/b02/a26>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110730/8873357e/attachment-0001.htm>

------------------------------

Message: 5
Date: Sat, 30 Jul 2011 22:58:55 +0300
From: Alexander Batischev <eual...@gmail.com>
Subject: Re: [Haskell-beginners] Haskell-style types in C or C++
To: beginners@haskell.org
Message-ID: <20110730195855.GC8469@speedy>
Content-Type: text/plain; charset="us-ascii"

Hi!

On Sat, Jul 30, 2011 at 10:45:06AM -0800, Christopher Howard wrote:
> One of the things that I love about Haskell is the syntax for
> creating user defined types. E.g.:
> 
> Data QueryResult = NoResult | DatabaseError String | Results [String]
> 
> I can prototype an entire program around these self-made types and
> it is a lot of fun. Out of intellect curiosity, though: what would
> be the equivalent construction in C or C++? (Say, for the type
> defined above).

Recently, same question occurred to me too, and the only solution I
could think of is defining base class (analog of data type name,
QueryResult in your case) and creating subclasses for each of data type'
constructors (NoResult, DatabaseError, Results). And you should resort
to templates if you want something like [a] as a parameter.

Google provides few nice links [1] [2] which may be helpful.

Just for your information, types like in the example above are called
Algebraic Data Types (ADT).

  1. http://cpp-next.com/archive/2010/09/algebraic-data-types-in-c/
  2. http://stackoverflow.com/questions/16770/haskells-algebraic-data-types

-- 
Regards,
Alexander Batischev

1024D/69093C81
F870 A381 B5F5 D2A1 1B35  4D63 A1A7 1C77 6909 3C81
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110730/52855a39/attachment-0001.pgp>

------------------------------

Message: 6
Date: Sat, 30 Jul 2011 23:19:28 +0300
From: Ovidiu Deac <ovidiud...@gmail.com>
Subject: [Haskell-beginners] Ambiguous type variable
To: beginners <beginners@haskell.org>
Message-ID:
        <cakvse7vyws+et0bnx9ujanstkx9im1u0spnldcr7fo6ixnq...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

I'm playing with Haskell so I wrote a stack module (see the code
below). I have a problem with the pop function which returns a tuple
(Nothing, EmptyStack) if called with an EmptyStack.

I kind of understand that the compiler cannot cannot figure out what
type to use for a. But how could I tell the compiler that if the list
is empty I don't care about that type?

Thanks,
Ovidiu

/////////////////
This is the hspec
...
    it "pop empty stack gives Nothing"
        ( (pop EmptyStack) ? (Nothing, EmptyStack))
...

This is the code:
module Stack where
import Prelude

data Stack a =
    EmptyStack |
    StackEntry a (Stack a)
    deriving(Show, Eq)
...
pop :: Stack a ?  (Maybe a, Stack a)
pop EmptyStack = (Nothing, EmptyStack)
pop (StackEntry a s) = ((Just a), s)

...and this is the error I get:
test/TestStack.hs:20:28:
    Ambiguous type variable `a0' in the constraint:
      (Eq a0) arising from a use of `=='
    Probable fix: add a type signature that fixes these type variable(s)
    In the second argument of `it', namely
      `((pop EmptyStack) == (Nothing, EmptyStack))'
    In the expression:
      it
        "pop empty stack gives Nothing"
        ((pop EmptyStack) == (Nothing, EmptyStack))
    In the second argument of `describe', namely
      `[it "empty stack is empty" (isEmpty EmptyStack),
        it
          "non-empty stack is not empty"
          (not (isEmpty (push 10 EmptyStack))),
        it
          "push then pop retrieves the same value"
          ((pop $ push 10 EmptyStack) == (Just 10, EmptyStack)),
        it
          "push push then pop retrieves the last value"
          ((pop $ push 2 (push 1 EmptyStack))
         ==
           (Just 2, (push 1 EmptyStack))),
        ....]'
make: *** [test] Error 1



------------------------------

Message: 7
Date: Sat, 30 Jul 2011 16:30:29 -0400
From: David McBride <dmcbr...@neondsl.com>
Subject: Re: [Haskell-beginners] Ambiguous type variable
To: Ovidiu Deac <ovidiud...@gmail.com>
Cc: beginners <beginners@haskell.org>
Message-ID:
        <can+tr42rjspi5yextqaptk_-etjwrtf7ln35g-rqk98dlnn...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

I can't run your code to be sure, but I think your problem is the ==.
It implies that whatever a is, it has an Eq constraint, but none of
your type signatures imply that.  Try this:

      case pop EmptyStack) of
        (Nothing, EmptyStack) -> blah
        otherwise -> do something else or nothing

That way you are using pattern matching instead of equality testing.

On Sat, Jul 30, 2011 at 4:19 PM, Ovidiu Deac <ovidiud...@gmail.com> wrote:
> I'm playing with Haskell so I wrote a stack module (see the code
> below). I have a problem with the pop function which returns a tuple
> (Nothing, EmptyStack) if called with an EmptyStack.
>
> I kind of understand that the compiler cannot cannot figure out what
> type to use for a. But how could I tell the compiler that if the list
> is empty I don't care about that type?
>
> Thanks,
> Ovidiu
>
> /////////////////
> This is the hspec
> ...
> ? ?it "pop empty stack gives Nothing"
> ? ? ? ?( (pop EmptyStack) ? (Nothing, EmptyStack))
> ...
>
> This is the code:
> module Stack where
> import Prelude
>
> data Stack a =
> ? ?EmptyStack |
> ? ?StackEntry a (Stack a)
> ? ?deriving(Show, Eq)
> ...
> pop :: Stack a ? ?(Maybe a, Stack a)
> pop EmptyStack = (Nothing, EmptyStack)
> pop (StackEntry a s) = ((Just a), s)
>
> ...and this is the error I get:
> test/TestStack.hs:20:28:
> ? ?Ambiguous type variable `a0' in the constraint:
> ? ? ?(Eq a0) arising from a use of `=='
> ? ?Probable fix: add a type signature that fixes these type variable(s)
> ? ?In the second argument of `it', namely
> ? ? ?`((pop EmptyStack) == (Nothing, EmptyStack))'
> ? ?In the expression:
> ? ? ?it
> ? ? ? ?"pop empty stack gives Nothing"
> ? ? ? ?((pop EmptyStack) == (Nothing, EmptyStack))
> ? ?In the second argument of `describe', namely
> ? ? ?`[it "empty stack is empty" (isEmpty EmptyStack),
> ? ? ? ?it
> ? ? ? ? ?"non-empty stack is not empty"
> ? ? ? ? ?(not (isEmpty (push 10 EmptyStack))),
> ? ? ? ?it
> ? ? ? ? ?"push then pop retrieves the same value"
> ? ? ? ? ?((pop $ push 10 EmptyStack) == (Just 10, EmptyStack)),
> ? ? ? ?it
> ? ? ? ? ?"push push then pop retrieves the last value"
> ? ? ? ? ?((pop $ push 2 (push 1 EmptyStack))
> ? ? ? ? ==
> ? ? ? ? ? (Just 2, (push 1 EmptyStack))),
> ? ? ? ?....]'
> make: *** [test] Error 1
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



------------------------------

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 37, Issue 67
*****************************************

Reply via email to