[Haskell-beginners] code explanation
Michael Orlitzky
michael at orlitzky.com
Sun Jul 3 15:32:06 CEST 2011
On 07/02/2011 12:47 PM, moowoo9 at fastmail.fm wrote:
> I'm a beginner, I'd like someone to help me understand a few lines of code
> My questions are:
> 1) Maybe String is an application of the constructor Maybe to the type
> String.
> What does it mean? Is there anything equivalent in the C language?
"Maybe" is the Haskell way to represent a value that might be Nothing
(you can think of Nothing as analogous to C's null). A value of type
(Maybe String) might be a String, or it might be Nothing. You usually
use pattern matching to determine which one you're dealing with before
you try to do anything with the value.
In C, this would be something like checking whether a string pointer is
null before you try to print it.
Homework:
http://learnyouahaskell.com/making-our-own-types-and-typeclasses
(I recommend the whole book if you have time. A hard copy is available
on Amazon.)
> 2) Is this a function with three arguments?
> *type T = [Char]*
> *type P = (Char, Maybe String)*
> *type Delta = ((Maybe Char, Char), Maybe String)*
> *fromGtoM :: T -> [P] -> [Delta]*
The type signature of this function shows both the arguments and its
return value. To learn more, you should read,
http://learnyouahaskell.com/higher-order-functions
but basically, you can think of the last value in the type signature
(e.g. [Delta], here) as the return value. The rest are arguments.
> 3) is this a usage of the above mentioned function?
> *fromGtoM t p = terminalRules ++ varRules*
We don't have enough information, but it doesn't look right.
> I dont'see why the function has three arguments but then only
> two are used t p.
> 4) what does *++* mean ?
The "++" function just concatenates two lists. Strings are lists, so you
might call it string concatenation too depending on the context.
More information about the Beginners
mailing list