You are not logged in.

#1 2009-09-27 01:16:02

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Some super-noob questions about Haskell [SOLVED]

Hurray, I'm back to finding time to start learning Haskell again! I have a few questions. The examples are from Write Yourself a Scheme in 48 hours:

http://en.wikibooks.org/wiki/Write_Your … n_48_Hours

Note: this code is importing Parsec. I guess that will be obvious to those that know what they're doing. Also, I hope the BBS doesn't butcher the indentation. Haskell doesn't need any more complications with its indentation (That was a joke. Don't yell at me).

The first question is on type signatures. This is one is fairly straight forward. It takes a String in and returns a String:

readExpr :: String -> String
readExpr input = case parse parseExpr "lisp" input of
    Left err -> "No match: " ++ show err
    Right _ -> "Found value"

The next one has got me a little confused in a couple of ways (data type LispVal is already defined for Strings):

parseString :: Parser LispVal
parseString = do char '"'
                 x <- many (noneOf "\"")
                 char '"'
                 return $ String x

parseExpr looks like this:

parseExpr :: Parser LispVal
parseExpr = parseAtom
        <|> parseString
        <|> parseNumber

I'm unclear on how to read parseString's type signature. I know that it's casting the return value to a LispVal String. It is telling me that it's casting the first String to a Parser String type, right? If so, I would have guessed that the signature would have looked something like this:

parseString :: Parser -> LispVal

What am I missing?

The other question is also in parseString. Am I reading correctly that the bit char '"' is swallowing the first quote, many( noneOf ) is grabbing up until the next, the second char '"' is swallowing the second quote, and that there shouldn't be anything else coming after?

Offline

#2 2009-09-28 23:25:47

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: Some super-noob questions about Haskell [SOLVED]

skottish wrote:

I'm unclear on how to read parseString's type signature. I know that it's casting the return value to a LispVal String. It is telling me that it's casting the first String to a Parser String type, right? If so, I would have guessed that the signature would have looked something like this:

parseString :: Parser -> LispVal

What am I missing?

It is not casting anything. The keyword that should rid you of your confusion is 'polymorphic types'.
In haskell you can define polymorphic data types like that (examples ripped from 'A Gentle Introduction to Haskell'):

data Point a            = Pt a a

In this case 'a' servers as a placeholder for any type, so you could do the following

Pt  2.0  3.0            :: Point Float
Pt  'a'  'b'            :: Point Char
Pt True False           :: Point Bool

So your function does actually return a 'Parser LispVal' whereas The function signature you gave above would take a Parser and Return a LispVal.

The other question is also in parseString. Am I reading correctly that the bit char '"' is swallowing the first quote, many( noneOf ) is grabbing up until the next, the second char '"' is swallowing the second quote, and that there shouldn't be anything else coming after?

Unless I am mistaken that is exactly what happens.


And btw: If you have any more questions I can really recommend #haskell on freenode. The guys there are very nice, helpful and much more knowledgeable than I am..

Offline

#3 2009-09-28 23:39:19

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Some super-noob questions about Haskell [SOLVED]

Thanks for the answer and the advice. It's funny, I told myself that if I got more than 100 looks with no answer, I'd ask at the IRC channel. I was just thinking of doing that, and...

So your function does actually return a 'Parser LispVal' whereas The function signature you gave above would take a Parser and Return a LispVal.

That makes sense. The author said basically what you said by my head was in the wrong place. Hearing it explained with more depth makes all the difference. Thanks for that.

Learning Haskell, albeit very slowly, has been remarkable. It's a beautiful language that really requires that one puts aside almost all concepts that one has ever learned. I'm glad that I took the "high road" and settled in on this text. I've learned more in the first ten pages of this book than I did in all of the other texts I looked at combined.  I'm sure RWH will come in as a good reference manual at some point, but the pace was boring to me.

Offline

#4 2009-09-28 23:47:40

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: Some super-noob questions about Haskell [SOLVED]

You are welcome :-)

Offline

Board footer

Powered by FluxBB