Utter Newbie - simple problems, output - GHC vs GHCi

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Utter Newbie - simple problems, output - GHC vs GHCi

Nigel Rantor

I am trying to get my head around Haskell but seem to keep butting
against problems that have nothing to do with FP yet, but are simply to
do with not understanding the tools.

I've been trying a lot of code from multiple tutorials but I keep
finding that the code simply does not work out of the box, and requires
some other setup I am unaware of.

I am currently on Debian, using GHC 6.8.2 installed using apt, so I
assume that the toolchain is installed and working correctly.

For example, the most recent tutorial I've been looking at is the "yet
another haskell tutorial", here -
http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf

One of the exercises after talking about functions that act on lists is
to determine the number of lowercase letters in a string.

Fine, that makes complete sense to me. I figure something along the
lines of:

length( filter( Char.isLower "LoweR" ) )

should return the value 3

If I attempt this at the interactive GHC prompt I get the following:

------------------------------------------------------------------------------------------
wiggly@mink:~/src/ht$ ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> show( length( filter( Char.isLower "LoweR" ) ) )

<interactive>:1:35:
    Couldn't match expected type `Char' against inferred type `[Char]'
    In the first argument of `GHC.Unicode.isLower', namely `"LoweR"'
    In the first argument of `filter', namely
        `(GHC.Unicode.isLower "LoweR")'
    In the first argument of `length', namely
        `(filter (GHC.Unicode.isLower "LoweR"))'
Prelude>
------------------------------------------------------------------------------------------

If I attempt to put the code into a file and compile it I get the following:

[Code]
------------------------------------------------------------------------------------------
module Main where
import Char
main = show( length( filter( Char.isLower "LoweR" ) ) )
------------------------------------------------------------------------------------------

[Terminal]
------------------------------------------------------------------------------------------
wiggly@mink:~/src/ht$ ghc -o test ex3.hs

ex3.hs:3:42:
    Couldn't match expected type `Char' against inferred type `[Char]'
    In the first argument of `isLower', namely `"LoweR"'
    In the first argument of `filter', namely `(isLower "LoweR")'
    In the first argument of `length', namely
        `(filter (isLower "LoweR"))'
wiggly@mink:~/src/ht$
------------------------------------------------------------------------------------------

This is one of the smallest examples I can think of posting for some
help, and quite frankly, I'm feeling a bit dim because I just cannot
understand why this doesn't work...I've tried in vain to mess with the
code (I won't attempt to describe it, I'm not au fait enough with
Haskell terminology yet, it would sound like gibberish *and* be incorrect)

Any help would be awesome, equally, any ready-to-run examples/tutorials
that people could recommend would likewise be awesome and beer-worthy[0].

Cheers,

  n

[0] offer only applies to those in the London area or those environs
where I find myself randomly
Reply | Threaded
Open this post in threaded view
|

Utter Newbie - simple problems, output - GHC vs GHCi

Magnus Therning
On Sun, Aug 30, 2009 at 11:09 PM, Nigel Rantor<[hidden email]> wrote:

>
> I am trying to get my head around Haskell but seem to keep butting
> against problems that have nothing to do with FP yet, but are simply to
> do with not understanding the tools.
>
> I've been trying a lot of code from multiple tutorials but I keep
> finding that the code simply does not work out of the box, and requires
> some other setup I am unaware of.
>
> I am currently on Debian, using GHC 6.8.2 installed using apt, so I
> assume that the toolchain is installed and working correctly.
>
> For example, the most recent tutorial I've been looking at is the "yet
> another haskell tutorial", here -
> http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf
>
> One of the exercises after talking about functions that act on lists is
> to determine the number of lowercase letters in a string.
>
> Fine, that makes complete sense to me. I figure something along the
> lines of:
>
> length( filter( Char.isLower "LoweR" ) )

try this instead

length (filter Char.isLower "LoweR")

`filter` takes two arguments.

/M

--
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus?therning?org          Jabber: magnus?therning?org
http://therning.org/magnus         identi.ca|twitter: magthe
Reply | Threaded
Open this post in threaded view
|

Utter Newbie - simple problems, output - GHC vs GHCi

Nigel Rantor
Magnus Therning wrote:
> On Sun, Aug 30, 2009 at 11:09 PM, Nigel Rantor<[hidden email]> wrote:
>>
>> length( filter( Char.isLower "LoweR" ) )
>
> try this instead
>
> length (filter Char.isLower "LoweR")
>
> `filter` takes two arguments.

Thank you, I have been flip-flopping between having no parentheses and
lots...I think I get it now.

I owe you a beer.

This will, however, probably not be my last post...

   n
Reply | Threaded
Open this post in threaded view
|

Utter Newbie - simple problems, output - GHC vs GHCi

Daniel Fischer-4
In reply to this post by Nigel Rantor
Am Montag 31 August 2009 00:09:41 schrieb Nigel Rantor:
> I am trying to get my head around Haskell but seem to keep butting
> against problems that have nothing to do with FP yet, but are simply to
> do with not understanding the tools.
>
> I've been trying a lot of code from multiple tutorials but I keep
> finding that the code simply does not work out of the box, and requires
> some other setup I am unaware of.

It's often old code which may have become obsolete by changes in the compiler and
libraries.

>
> I am currently on Debian, using GHC 6.8.2 installed using apt, so I
> assume that the toolchain is installed and working correctly.
>
> For example, the most recent tutorial I've been looking at is the "yet
> another haskell tutorial", here -
> http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf
>
> One of the exercises after talking about functions that act on lists is
> to determine the number of lowercase letters in a string.
>
> Fine, that makes complete sense to me. I figure something along the
> lines of:
>
> length( filter( Char.isLower "LoweR" ) )
>
> should return the value 3
>
> If I attempt this at the interactive GHC prompt I get the following:
>
> ---------------------------------------------------------------------------
>--------------- wiggly@mink:~/src/ht$ ghci
> GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
> Loading package base ... linking ... done.
> Prelude> show( length( filter( Char.isLower "LoweR" ) ) )
>
> <interactive>:1:35:
>     Couldn't match expected type `Char' against inferred type `[Char]'
>     In the first argument of `GHC.Unicode.isLower', namely `"LoweR"'
>     In the first argument of `filter', namely
>         `(GHC.Unicode.isLower "LoweR")'
>     In the first argument of `length', namely
>         `(filter (GHC.Unicode.isLower "LoweR"))'
> Prelude>
> ---------------------------------------------------------------------------
>---------------

Wrong parentheses. 'filter' takes two arguments, the predicate by which to filter and the
list to be filtered. With your parentheses, you pass it only one argument, namely

(Data.Char.isLower "LoweR")

However, isLower takes a Char as argument, while here it is given a String, this is the
type error reported by ghci.
What you wanted is

length (filter Data.Char.isLower "LoweR")

Function application doesn't need parentheses in Haskell, so a multi-argument function is
called

function arg1 arg2 arg3

and not function(arg1, arg2, arg3)

as in most imperative languages. It takes a bit getting used to.

You don't need the 'show', by the way, ghci prints the result of the evaluation of an
expression typed at the prompt anyway.
And, while the modules Char, List, IO etc. still exist, the compiler now uses hierarchical
modules and they now live in Data.Char, Data.List, System.IO etc. Better to get the habit
of using hierarchical modules from the start.

>
> If I attempt to put the code into a file and compile it I get the
> following:
>
> [Code]
> ---------------------------------------------------------------------------
>--------------- module Main where
> import Char
> main = show( length( filter( Char.isLower "LoweR" ) ) )
> ---------------------------------------------------------------------------
>---------------

Same parenthisation issue as above, on top of that, main must have type IO (), but

show whatEver

is a String. It would be

import Data.Char

main = print (length (filter isLower "LoweR"))

>
> [Terminal]
> ---------------------------------------------------------------------------
>--------------- wiggly@mink:~/src/ht$ ghc -o test ex3.hs
>
> ex3.hs:3:42:
>     Couldn't match expected type `Char' against inferred type `[Char]'
>     In the first argument of `isLower', namely `"LoweR"'
>     In the first argument of `filter', namely `(isLower "LoweR")'
>     In the first argument of `length', namely
>         `(filter (isLower "LoweR"))'
> wiggly@mink:~/src/ht$
> ---------------------------------------------------------------------------
>---------------
>
> This is one of the smallest examples I can think of posting for some
> help, and quite frankly, I'm feeling a bit dim because I just cannot
> understand why this doesn't work...I've tried in vain to mess with the
> code (I won't attempt to describe it, I'm not au fait enough with
> Haskell terminology yet, it would sound like gibberish *and* be incorrect)
>
> Any help would be awesome, equally, any ready-to-run examples/tutorials
> that people could recommend would likewise be awesome and beer-worthy[0].
>

Try the wikibook:

http://en.wikibooks.org/wiki/Haskell

it contains (almost?) all of YAHT and much more. It's still being written, but it's
already very usable.

Also, you might take a look at http://book.realworldhaskell.org/ .

> Cheers,
>
>   n
>
> [0] offer only applies to those in the London area or those environs
> where I find myself randomly

Dang. Could've done with a beer :(

Reply | Threaded
Open this post in threaded view
|

Utter Newbie - simple problems, output - GHC vs GHCi

Nigel Rantor
Daniel Fischer wrote:
> Am Montag 31 August 2009 00:09:41 schrieb Nigel Rantor:
>> I am trying to get my head around Haskell but seem to keep butting
>> against problems that have nothing to do with FP yet, but are simply to
>> do with not understanding the tools.
[snip]

>> Any help would be awesome, equally, any ready-to-run examples/tutorials
>> that people could recommend would likewise be awesome and beer-worthy[0].
>>
>
> Try the wikibook:
>
> http://en.wikibooks.org/wiki/Haskell
>
> it contains (almost?) all of YAHT and much more. It's still being written, but it's
> already very usable.
>
> Also, you might take a look at http://book.realworldhaskell.org/ .

Thanks for this, the extra info around my problems is a big help, and
makes me feel slightly less frustrated.

>> Cheers,
>>
>>   n
>>
>> [0] offer only applies to those in the London area or those environs
>> where I find myself randomly
>
> Dang. Could've done with a beer :(

You never know, I may be in a town near you soon...and, also, I'm liable
to be asking for more help. If anyone gets 12 points I'll ship a case...

   n
Reply | Threaded
Open this post in threaded view
|

Utter Newbie - simple problems, output - GHC vs GHCi

Magnus Therning
In reply to this post by Nigel Rantor
On Mon, Aug 31, 2009 at 12:32 AM, Nigel Rantor<[hidden email]> wrote:

> Magnus Therning wrote:
>> On Sun, Aug 30, 2009 at 11:09 PM, Nigel Rantor<[hidden email]> wrote:
>>>
>>> length( filter( Char.isLower "LoweR" ) )
>>
>> try this instead
>>
>> length (filter Char.isLower "LoweR")
>>
>> `filter` takes two arguments.
>
> Thank you, I have been flip-flopping between having no parentheses and
> lots...I think I get it now.
>
> I owe you a beer.
>
> This will, however, probably not be my last post...

I'm most often in Cambridge, but do find myself in London
occasionally.  Adding more beers to the first one is nothing I shy
away from :-)

/M

--
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus?therning?org          Jabber: magnus?therning?org
http://therning.org/magnus         identi.ca|twitter: magthe