[Haskell-beginners] Utter Newbie - simple problems, output - GHC vs GHCi

Daniel Fischer daniel.is.fischer at web.de
Sun Aug 30 18:48:03 EDT 2009


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 at 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 at 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 at 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 :(



More information about the Beginners mailing list