- FINALLY a language (that I'm aware of) where True !== 1 and False !== 0. I'd have to take off YOUR shoes to count the number of times that's bitten me in the ass. For people who don't know me, I wear sandals so no shoe-taking-offing is ever needed.
- The it primitive is pretty neat and has possibilities:
True || False
>>True
it
>>True
not it
>>False
- I like the /= representation of !=
- Hmmm...lists are interesting.
let x = [1,2]
[1,2]
0:x
[0,1,2]
So clearly the : primitive appends elements to the beginning of a list. I thought that perhaps [0,1]:x might produce [[0,1],[1,2]]. It turns out this is not the case, and instead throws an error that essentially amount to a data type mismatch. To achieve the desired result, one must type:
[0,1]:[x]
...such that both arguments are lists, I assume. Weird. -
putStrLn "Hi"
Hi
putStrLn 'Hi'
:1:11:
lexical error in string/character literal at character 'i'
D'oh. I prefer to use single quotes with my strings. Not a deal breaker though.
Well, as thrilling as I'm sure this has been for you, I'm going to stop and actually get to coding.
Hey there. One thing you might find useful in ghci (and hugs i think, not sure which you're using) is the :t command. if you run :t x, you'll see it is something like "(Num t) => [t]". also, if you use :t (:), you'll see that : has the type a -> [a] -> [a]. which said it takes an a, and a list of a's and produces a list of as (with your element added to the head of the list.).
If you try and do y:x, you should see that y has to be a number, so y :: a, x :: [a]. when you do [0,1]:x, you're trying to make : work with [a] -> [a] -> [a] (or, [a] -> [a] -> [[a]] as you implied), which doesn't make sense.
When you do [0,1]:[x], you'll see that [x] has the type [x] :: (Num t) => [[t]], with [0,1] having [t], so in this case, your a = [t]
: just adds an element to the head of a list list of elements of that type, it doesn't do anything fancy like making a new list when the types don't match.
Hope this helps. if you have questions about haskell, come join us on irc at #haskell on irc.freenode.net.
Cheers,
Axman6
Always interesting to see new Haskell users!
The thing about : is that its right parameter must be a list of elements of the same type as its left parameter. So if its left parameter is a list of integers, then its right parameter must be a list of lists of integers.
Single quotes are used to denote characters, as opposed to strings (which are lists of characters). One wouldn't want to have difficulty telling whether something was a Char or a String.
If you haven't already visited us on IRC, feel free to come and hang out in #haskell on irc.freenode.net -- there are usually plenty of people around who are happy to help out beginners.
The (:) operator requires the second argument to be a list and the first to be the same type as the elements of that list. For example:
--These are ok
> 5:[1,2,3]
[5,1,2,3]
> [12,3,4]:[[],[1,2,3]]
[[12,3,4],[],[1,2,3]]
--Not OK
>[1]:[2,3,5]
To add to the right side you should probably use the (++) operator.
Good luck with Haskell and prepare to have your mind blown several times. :)