Send Beginners mailing list submissions to
        [email protected]

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
        [email protected]

You can reach the person managing the list at
        [email protected]

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


Today's Topics:

   1. Re:  ~ type operator (Guy)
   2.  A first try (Panagiotis Koutsourakis)
   3. Re:  A first try (David Place)
   4. Re:  Getting Stuff out of IO Handles (Raphael P?bst)
   5. Re:  A first try (Panagiotis Koutsourakis)
   6. Re:  ~ type operator (James Cook)
   7. Re:  Would you shed light on "undefined values" please?
      (Brent Yorgey)
   8. Re:  A first try (Jared Hance)
   9. Re:  A first try (Panagiotis Koutsourakis)


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

Message: 1
Date: Fri, 24 Jun 2011 14:44:36 +0300
From: Guy <[email protected]>
Subject: Re: [Haskell-beginners] ~ type operator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 24/06/2011 12:10, Daniel Fischer wrote:
> On Friday 24 June 2011, 10:26:42, Guy wrote:
>> What does the ~ type operator mean? I've sometimes seen types such as (a
>> ~ b) in error messages, but can't understand what GHC is trying to tell
>> me.
>
> Type equality, (a ~ b) means that a and b are the same type (rather, that
> the compiler can prove they're the same).

So where is it useful to say a ~ b, rather than using a twice?




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

Message: 2
Date: Fri, 24 Jun 2011 13:48:03 +0200
From: Panagiotis Koutsourakis <[email protected]>
Subject: [Haskell-beginners] A first try
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello all,

I've been spending some time recently learning Haskell. I am not a
newbie in programming, and I've had some experience with lisp in the
past, but I have not really studied functional programming. I am
mostly seeking feedback regarding style, and glaring mistakes that I
may make in some toy examples, but I am not sure if this is the proper
place to ask.

In case it is though, here is an attempt (not yet complete) at the
word count program:

http://pastebin.com/JZw22QGA

Thanks in advance!

-- 
Panagiotis Koutsourakis



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

Message: 3
Date: Fri, 24 Jun 2011 08:43:59 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: Panagiotis Koutsourakis <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi, Panagiotis.

The wc program is a good choice for a learning program, but it reveals a 
long-standing weakness in Haskell: Lazy-IO.  I think you will find that your 
program will get an error if you try to run wc on many files.  You will open 
too many files at once.  

There is a solution called IterateeIO.  It's extremely elegant and very good 
Haskell, but quite challenging to understand.  So far, I have not found a good 
tutorial.

Luck for you the best example program is an implementation of wc!

I posted it in hpaste.org.  (A Haskell-specific version of pasteBin.)

> http://hpaste.org/48254

Cheers,
David

____________________
David Place   
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]



On Jun 24, 2011, at 7:48 AM, Panagiotis Koutsourakis wrote:

> Hello all,
> 
> I've been spending some time recently learning Haskell. I am not a
> newbie in programming, and I've had some experience with lisp in the
> past, but I have not really studied functional programming. I am
> mostly seeking feedback regarding style, and glaring mistakes that I
> may make in some toy examples, but I am not sure if this is the proper
> place to ask.
> 
> In case it is though, here is an attempt (not yet complete) at the
> word count program:
> 
> http://pastebin.com/JZw22QGA
> 
> Thanks in advance!
> 
> -- 
> Panagiotis Koutsourakis
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




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

Message: 4
Date: Fri, 24 Jun 2011 15:07:38 +0200
From: Raphael P?bst <[email protected]>
Subject: Re: [Haskell-beginners] Getting Stuff out of IO Handles
To: Kyle Murphy <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Thanks for your replies!
A short example of what I'm doing:

handle <- connectTo ... --opening the handle on a tcp connection
hPut handle message -- sending a ByteString
answer <- hGet handle n -- getting the answer back from the server
hClose handle

I have now several things I would like to do with the answer and my
question is, if I should put the functions into the handle or if I can
make the function with the handle of a type that returns answer, not
of IO () as it is currently.

Does this make sense? I hope so.

Raf

On 6/24/11, Kyle Murphy <[email protected]> wrote:
> I had started to say something about the circumstances under which you
> actually can use unsafePerformIO, but it sort of bogged down the reply and
> muddled the point I was trying to make. You seem to have done a better job
> explaining the circumstances under which you can actually use unsafePerfomIO
> safely than I was able to at the time.
>
>
> -R. Kyle Murphy
> --
> Curiosity was framed, Ignorance killed the cat.
>
>
> On Thu, Jun 23, 2011 at 19:46, Jared Hance <[email protected]> wrote:
>
>> > If you want to know if you can do something like:
>> >
>> > someFunction :: Int -> Int
>> > someFunction n = escapeIO $ someIOAction n
>> >
>> > then the answer is you shouldn't. It is technically possible, by using a
>> > function that every experienced haskell developer will tell you to never
>> > ever ever ever ever ever ever ever (get the picture) use, which is
>> > unsafePerformIO, but if you use unsafePerformIO it's likely that your
>> code
>> > won't do what you actually expect it to do, even if the type signatures
>> all
>> > check correctly. The IO Monad serves a very important purpose and
>> > implies
>> > certain things about a computation, likewise the absence of the IO Monad
>> > implies certain guarantees, and when you break those guarantees bad
>> things
>> > happen.
>>
>> I'd like to clarify a bit here. The unsafePerformIO function has a use,
>> and should be used, but only in the right circumstances. The problem is
>> that often a function is referentially transparent, but haskell cannot
>> prove that it is because it uses the IO Monad.
>>
>> The circumstance in which this crops up is usually when you use the FFI.
>> The function you call in C might return a pointer to something, rather
>> than the data itself. The pointer will usually be different every time,
>> so the result isn't referentially transparent. However, if you extract
>> the data from the pointer, that data _is_ referentially transparent and
>> is, as such, a good use case for unsafePerformIO.
>>
>> So I would say an experienced programmer would say to never use it if
>> you don't know what you are doing, but if you do, use it when you need
>> it.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>



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

Message: 5
Date: Fri, 24 Jun 2011 15:22:25 +0200
From: Panagiotis Koutsourakis <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: David Place <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi David,

Thanks very much for your reply. I'll take a look at IterateIO and I
didn't know about hpaste.org, so i'll check it out as well.

On Fri, Jun 24, 2011 at 2:43 PM, David Place <[email protected]> wrote:
> Hi, Panagiotis.
>
> The wc program is a good choice for a learning program, but it reveals a 
> long-standing weakness in Haskell: Lazy-IO. ?I think you will find that your 
> program will get an error if you try to run wc on many files. ?You will open 
> too many files at once.
>
> There is a solution called IterateeIO. ?It's extremely elegant and very good 
> Haskell, but quite challenging to understand. ?So far, I have not found a 
> good tutorial.
>
> Luck for you the best example program is an implementation of wc!
>
> I posted it in hpaste.org. ?(A Haskell-specific version of pasteBin.)
>
>> http://hpaste.org/48254
>
> Cheers,
> David
>
> ____________________
> David Place
> Owner, Panpipes Ho! LLC
> http://panpipesho.com
> [email protected]
>
>
>
> On Jun 24, 2011, at 7:48 AM, Panagiotis Koutsourakis wrote:
>
>> Hello all,
>>
>> I've been spending some time recently learning Haskell. I am not a
>> newbie in programming, and I've had some experience with lisp in the
>> past, but I have not really studied functional programming. I am
>> mostly seeking feedback regarding style, and glaring mistakes that I
>> may make in some toy examples, but I am not sure if this is the proper
>> place to ask.
>>
>> In case it is though, here is an attempt (not yet complete) at the
>> word count program:
>>
>> http://pastebin.com/JZw22QGA
>>
>> Thanks in advance!
>>
>> --
>> Panagiotis Koutsourakis
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>



-- 
Panagiotis Koutsourakis



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

Message: 6
Date: Fri, 24 Jun 2011 10:34:37 -0400
From: James Cook <[email protected]>
Subject: Re: [Haskell-beginners] ~ type operator
To: Guy <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

On Jun 24, 2011, at 7:44 AM, Guy wrote:

> On 24/06/2011 12:10, Daniel Fischer wrote:
>> On Friday 24 June 2011, 10:26:42, Guy wrote:
>>> What does the ~ type operator mean? I've sometimes seen types such  
>>> as (a
>>> ~ b) in error messages, but can't understand what GHC is trying to  
>>> tell
>>> me.
>>
>> Type equality, (a ~ b) means that a and b are the same type  
>> (rather, that
>> the compiler can prove they're the same).
>
> So where is it useful to say a ~ b, rather than using a twice?

The original motivation, I believe, was for use with a some of the  
more advanced type system extensions: type families and GADTs   
(apologies if you haven't run across these before; I'm not going to  
give a detailed explanation of them because Google already has that  
covered, just a couple of examples assuming familiarity with them).

In code using the vector-space[1] package, you might wish to use a  
constraint such as:

 > Scalar a ~ Scalar b => ...

Scalar is essentially a type-level function, and this constraint  
asserts that it maps two different types to the same type.

In the process of type-checking code using GADTs, GHC will also  
"discover" type equalities that may not be obvious.  For example,

 > data Eq a b where Refl :: Eq a a
 >
 > coerce :: Eq a b -> a -> b
 > coerce Refl x = x

When checking this code, GHC infers that within the definition of  
'coerce', x has a type something like "a ~ b => a".  Which is to say,  
'a' and 'b' are still distinct type variables, but they happen to  
provably have the same value (in this particular context).

-- James

[1] http://hackage.haskell.org/package/vector-space



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

Message: 7
Date: Fri, 24 Jun 2011 07:44:49 -0700
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Would you shed light on "undefined
        values" please?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Jun 24, 2011 at 08:13:26AM +0200, Ertugrul Soeylemez wrote:
> 
> Bottom arises, as soon as a language is total.  This is related to the
> halting problem.  Bottom is the value, which is never computed in the
> sense that it never results.  Only nontotal languages (like Agda) can
> prevent bottom values from appearing.

That is a funny use of the word "total"; I think you mean
"Turing-complete".  "Total" is usually used of languages in which it
is only possible to define total functions -- such as Agda.  Haskell
is non-total since it is possible to define non-total, i.e. partial,
functions, which are undefined for some inputs.

-Brent




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

Message: 8
Date: Fri, 24 Jun 2011 10:51:26 -0400
From: Jared Hance <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: [email protected]
Message-ID: <20110624145126.GB2455@gentoo>
Content-Type: text/plain; charset=us-ascii

Okay, I'm going to give some more stylistic notes.

> 8. let combined = parseArgs Args

You don't need this one.

> Lines 9 - 11
> let sep = separateFiles args
>      fl = head (fst sep)
> showResultsFile fl (snd sep)

Why not:

let (f, s) = separateFiles args
showResultsFile (head f) s

> Lines 19-20
> processFile fl flags =
>   mapM dispatch [(flag, fl) | flag <- flags]

Why isn't this on one line? Its not particularly long.

> Lines 25-31
> dispatch (fl, fn) = do
>   handle <- openFile fl ReadMode
>   contents <- hGetContents handle
>   let cnt = getCounter fn
>       res = cnt contents
>   hClose handle
>   return res

Cleaner is:

dispatch (fl, fn) = do contents <- withFile fl ReadMode hGetContents
                       return $ getCounter fn contents

However also note that you are defining a function of the form

f :: (a, b) -> c

rather than the better

f :: a -> b -> c

Naturally, I see that the reason is that you are calling it from mapM.
However, I would define dispatch as a curried function, and then use
`curry dispatch` in your mapM. It makes dispatch much cleaner.

> Lines 36-44
> a bunch of putStrLn

You could also use mapM_ putStrLn [list of string to put out], removing
duplicate uses of putStrLn here.



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

Message: 9
Date: Fri, 24 Jun 2011 17:45:54 +0200
From: Panagiotis Koutsourakis <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: Jared Hance <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Thank you very much Jared. Much appreciated.

On Fri, Jun 24, 2011 at 4:51 PM, Jared Hance <[email protected]> wrote:
> Okay, I'm going to give some more stylistic notes.
>
>> 8. let combined = parseArgs Args
>
> You don't need this one.
>
>> Lines 9 - 11
>> let sep = separateFiles args
>> ? ? ?fl = head (fst sep)
>> showResultsFile fl (snd sep)
>
> Why not:
>
> let (f, s) = separateFiles args
> showResultsFile (head f) s
>
>> Lines 19-20
>> processFile fl flags =
>> ? mapM dispatch [(flag, fl) | flag <- flags]
>
> Why isn't this on one line? Its not particularly long.
>
>> Lines 25-31
>> dispatch (fl, fn) = do
>> ? handle <- openFile fl ReadMode
>> ? contents <- hGetContents handle
>> ? let cnt = getCounter fn
>> ? ? ? res = cnt contents
>> ? hClose handle
>> ? return res
>
> Cleaner is:
>
> dispatch (fl, fn) = do contents <- withFile fl ReadMode hGetContents
> ? ? ? ? ? ? ? ? ? ? ? return $ getCounter fn contents
>
> However also note that you are defining a function of the form
>
> f :: (a, b) -> c
>
> rather than the better
>
> f :: a -> b -> c
>
> Naturally, I see that the reason is that you are calling it from mapM.
> However, I would define dispatch as a curried function, and then use
> `curry dispatch` in your mapM. It makes dispatch much cleaner.
>
>> Lines 36-44
>> a bunch of putStrLn
>
> You could also use mapM_ putStrLn [list of string to put out], removing
> duplicate uses of putStrLn here.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Panagiotis Koutsourakis



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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 36, Issue 61
*****************************************

Reply via email to