Question on syntax

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

Question on syntax

Rohit Sharma
All,

I started learning haskell very recently and have a question on the pattern
matching style for lists.

In the below snippet i.e. "(x:xs)" why do we went with round braces and not
square? I know we are using cons that tells this is not a tuple but would
it not make more sense to write something like [x:xs] instead of (x:xs), i
thought round braces was used for pair/tuples?

safeHead [] = Nothing
safeHead (x:xs) = Just x

Thanks,
Rohit
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141025/00248e82/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Question on syntax

Bob Ippolito
Round braces are used for grouping, it's necessary to avoid ambiguity since
a function can be defined with more than one argument. When using case you
don't have this potential ambiguity, so don't need the parentheses. Tuples
are more of a special case in the grammar that isn't closely related to
this.

safeHead p = case p of
  [] -> Nothing
  x : xs -> Just x

On Friday, October 24, 2014, Rohit Sharma <rohits79 at gmail.com> wrote:

> All,
>
> I started learning haskell very recently and have a question on the
> pattern matching style for lists.
>
> In the below snippet i.e. "(x:xs)" why do we went with round braces and
> not square? I know we are using cons that tells this is not a tuple but
> would it not make more sense to write something like [x:xs] instead of
> (x:xs), i thought round braces was used for pair/tuples?
>
> safeHead [] = Nothing
> safeHead (x:xs) = Just x
>
> Thanks,
> Rohit
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141024/3a340d8a/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Question on syntax

Rustom Mody
In reply to this post by Rohit Sharma
On Sat, Oct 25, 2014 at 9:01 AM, Rohit Sharma <rohits79 at gmail.com> wrote:

> All,
>
> I started learning haskell very recently and have a question on the
> pattern matching style for lists.
>
> In the below snippet i.e. "(x:xs)" why do we went with round braces and
> not square? I know we are using cons that tells this is not a tuple but
> would it not make more sense to write something like [x:xs] instead of
> (x:xs), i thought round braces was used for pair/tuples?
>
> safeHead [] = Nothing
> safeHead (x:xs) = Just x
>
>
Yes this can be confusing.

Lets break it into two separate questions:
1. Why [] is not used around x:xs
2. Why () is used

To address 1 start ghci and try out these expressions

1: [2,3]

[1:[2,3]]


To address 2 try out

length [1,2] ++ [3,4]

and then

length ([1,2] ++ [3,4])

Another related example

sin pi/2

and
sin (pi/2)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141025/91725df3/attachment.html>