Top beginner mistakes

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

Top beginner mistakes

7stud-2
Here are my top three:

1) Putting an equals sign after a function name when using guards:

myfunction x y =
    | x < 2              = "a"
    | y > 20             = "b"
    | otherwise          = "c"


dhask.hs:2:4: parse error on input `|'
Failed, modules loaded: none.



2) Writing "if" in front of the guard conditions:

myfunction x y =
    | if x < 2           = "a"
    | if y > 20          = "b"
    | otherwise          = "c"


dhask.hs:2:25: parse error on input `='
Failed, modules loaded: none.



3) Not putting parentheses around arguments of the form x:xs

dosomething x:xs = head xs

dhask.hs:1:0: Parse error in pattern
Failed, modules loaded: none.


In all three cases, the error messages don't help spot the problem.



Reply | Threaded
Open this post in threaded view
|

Re: Top beginner mistakes

Benjamin L. Russell
Those are excellent examples!

I have just added them to HaskellWiki on the following page, and
credited them to you:

Common Misunderstandings - HaskellWiki
http://www.haskell.org/haskellwiki/Common_Misunderstandings

Good work!

-- Benjamin L. Russell

On Wed, 4 Mar 2009 21:54:05 +0000 (UTC), 7stud
<[hidden email]> wrote:

>Here are my top three:
>
>1) Putting an equals sign after a function name when using guards:
>
>myfunction x y =
>    | x < 2              = "a"
>    | y > 20             = "b"
>    | otherwise          = "c"
>
>
>dhask.hs:2:4: parse error on input `|'
>Failed, modules loaded: none.
>
>
>
>2) Writing "if" in front of the guard conditions:
>
>myfunction x y =
>    | if x < 2           = "a"
>    | if y > 20          = "b"
>    | otherwise          = "c"
>
>
>dhask.hs:2:25: parse error on input `='
>Failed, modules loaded: none.
>
>
>
>3) Not putting parentheses around arguments of the form x:xs
>
>dosomething x:xs = head xs
>
>dhask.hs:1:0: Parse error in pattern
>Failed, modules loaded: none.
>
>
>In all three cases, the error messages don't help spot the problem.
--
Benjamin L. Russell  /   DekuDekuplex at Yahoo dot com
http://dekudekuplex.wordpress.com/
Translator/Interpreter / Mobile:  +011 81 80-3603-6725
"Furuike ya, kawazu tobikomu mizu no oto."
-- Matsuo Basho^

Reply | Threaded
Open this post in threaded view
|

Re: Top beginner mistakes

7stud-2
Benjamin L. Russell <DekuDekuplex <at> Yahoo.com> writes:

> Those are excellent examples!
>
> I have just added them to HaskellWiki on the following page, and
> credited them to you:
>
> Common Misunderstandings - HaskellWiki
> http://www.haskell.org/haskellwiki/Common_Misunderstandings
>
> Good work!
>

Thanks!  I made a copy and paste error though.  The second example
still has the equals sign:

myfunction x y  =

which would produce the same error message as the first example.
The equals sign should be stricken from the second example.

Also, no quotes around 7stud in the attribution! lol.








Reply | Threaded
Open this post in threaded view
|

Re: Top beginner mistakes

Benjamin L. Russell
On Thu, 5 Mar 2009 12:19:55 +0000 (UTC), 7stud
<[hidden email]> wrote:

>Benjamin L. Russell <DekuDekuplex <at> Yahoo.com> writes:
>> Those are excellent examples!
>>
>> I have just added them to HaskellWiki on the following page, and
>> credited them to you:
>>
>> Common Misunderstandings - HaskellWiki
>> http://www.haskell.org/haskellwiki/Common_Misunderstandings
>>
>> Good work!
>>
>
>Thanks!  I made a copy and paste error though.  The second example
>still has the equals sign:
>
>myfunction x y  =
>
>which would produce the same error message as the first example.
>The equals sign should be stricken from the second example.

Stricken.

>Also, no quotes around 7stud in the attribution! lol.

Quotes removed.

Thanks again!

-- Benjamin L. Russell
--
Benjamin L. Russell  /   DekuDekuplex at Yahoo dot com
http://dekudekuplex.wordpress.com/
Translator/Interpreter / Mobile:  +011 81 80-3603-6725
"Furuike ya, kawazu tobikomu mizu no oto."
-- Matsuo Basho^

Reply | Threaded
Open this post in threaded view
|

Top beginner mistakes

Francesco Bochicchio
In reply to this post by 7stud-2
2009/3/4 7stud <[hidden email]>

> Here are my top three:
>
> ...




>
> 3) Not putting parentheses around arguments of the form x:xs
>
> dosomething x:xs = head xs
>
>
>
I have a variation here:

function [] = ...
function [x:xs] = ...

Another of mine is the classical:

print function arguments -- or variations

but I finally learned to use '$' here .



>
>
> In all three cases, the error messages don't help spot the problem.
>
>
> I think that part of the problem  is that haskell syntax is powerful but
terse, and therefore is not easy for a compiler to guess the intensions in
the programmers.
For instance, in any other language the expression 'map f'  would give the
error "missing second parameter",  while in haskell could be a partial
function application.

However, some of ghc messages could probably  be made more
'beginners-friendly'.

Ciao
---------
FB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090305/ced87139/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Top beginner mistakes

Daniel Fischer-4
Am Donnerstag, 5. M?rz 2009 19:13 schrieb Francesco Bochicchio:

> 2009/3/4 7stud <[hidden email]>
>
> > Here are my top three:
> >
> > ...
> >
> > 3) Not putting parentheses around arguments of the form x:xs
> >
> > dosomething x:xs = head xs
>
> I have a variation here:
>
> function [] = ...
> function [x:xs] = ...
>
> Another of mine is the classical:
>
> print function arguments -- or variations
>
> but I finally learned to use '$' here .
>
> > In all three cases, the error messages don't help spot the problem.
> >

At least they give a source position, so you know where to look.
But yes, "parse error" is not very specific.

> >
> > I think that part of the problem  is that haskell syntax is powerful but
>
> terse, and therefore is not easy for a compiler to guess the intentions in
> the programmers.
> For instance, in any other language the expression 'map f'  would give the
> error "missing second parameter",  while in haskell could be a partial
> function application.
>
> However, some of ghc messages could probably  be made more
> 'beginners-friendly'.

Create a ticket if you encounter a particularly unhelpful error message.
If you can suggest a better message, all the better.

>
> Ciao
> ---------
> FB

Cheers,
Daniel

Reply | Threaded
Open this post in threaded view
|

Re: Top beginner mistakes

7stud-2
In reply to this post by Benjamin L. Russell
Benjamin L. Russell <DekuDekuplex <at> Yahoo.com> writes:
> >>
> >> Common Misunderstandings - HaskellWiki
> >> http://www.haskell.org/haskellwiki/Common_Misunderstandings
> >>

I thought I'd point out something wrong with the first beginner
mistake listed on that page:

--------
1.1  Indentation
...
...
What some miss is that then and else must be indented deeper
than the if statement:

if boolean
    then expr1
    else expr2
----------

My tests show that the 'then' and 'else' do not have to be indented
more than the 'if':

--bhask.hs

myfunc x = if x == 2
           then "hello"
           else "goodbye"


Prelude> :load bhask.hs
[1 of 1] Compiling Main             ( bhask.hs, interpreted )
Ok, modules loaded: Main.
*Main> myfunc 2
"hello"
*Main> myfunc 3
"goodbye"



In fact, 'then' and 'else' work for me when they are indented less
than the 'if':

--bhask.hs

myfunc x = if x == 2
     then "hello"
     else "goodbye"


*Main> :load bhask.hs
[1 of 1] Compiling Main             ( bhask.hs, interpreted )
Ok, modules loaded: Main.
*Main> myfunc 4
"goodbye"
*Main> myfunc 5
"goodbye"
*Main> myfunc 2
"hello"


I don't if haskell changed its indenting rules or what, but that
tip seems to be erroneous.





Reply | Threaded
Open this post in threaded view
|

Re: Top beginner mistakes

Brandon S Allbery KF8NH
On 2009 Mar 8, at 21:55, 7stud wrote:
> Benjamin L. Russell <DekuDekuplex <at> Yahoo.com> writes:
>>>> Common Misunderstandings - HaskellWiki
>>>> http://www.haskell.org/haskellwiki/Common_Misunderstandings
>
> I thought I'd point out something wrong with the first beginner
> mistake listed on that page:
>
> My tests show that the 'then' and 'else' do not have to be indented
> more than the 'if':

They do, but only within layout, i.e. a "let" or "do", because  
otherwise they fall outside the layout block.

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090309/010df599/PGP.bin