The (x:xs) in function parameter is a tuple?

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

The (x:xs) in function parameter is a tuple?

Nan Xiao
Hi all,

Greetings from me!

I am confused about the function parameters and tuple. E.g.:

occurs value [] = 0
occurs value (x:xs) = (if value == x then 1 else 0) + occurs value xs

should we consider (x:xs) as a tuple?

Thanks in advance!

Best Regards
Nan Xiao
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)
(:) is a constructor. For example, you can define lists as:

data List a = Nil | Cons a (List a)

GHC does some magic to provide us with the same definition, but with Nil replaced by [] and Cons replaced by (:).

As constructors can be pattern matched on, you can also match on a (:), which is a data constructor.

You might consider (x:xs) as a tuple, only if you're willing to consider (Cons x xs) as a tuple. It is a tuple (ordered collection of two values), but not a tuple according to their definition in haskell.
What kind of tuple are you talking about?

Hope this helps.

Regards,
  Sumit

On 24 February 2016 at 16:01, Nan Xiao <[hidden email]> wrote:
Hi all,

Greetings from me!

I am confused about the function parameters and tuple. E.g.:

occurs value [] = 0
occurs value (x:xs) = (if value == x then 1 else 0) + occurs value xs

should we consider (x:xs) as a tuple?

Thanks in advance!

Best Regards
Nan Xiao
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Imants Cekusins
In reply to this post by Nan Xiao
Hello Xiao,

(one_element)             -- is evaluation
(element,element,...)    -- is tuple

(1:[2])     -- [1,2] because it is one "array element"
(1,2)      -- is a tuple because there are 2 elements
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Rein Henrichs
A tuple can have any number of elements. (1,2) is a 2-tuple. (1,2,3) is a 3-tuple. (x:xs) and (x,y) are differently typed: xs must be a list of the type of x, while y can be any type. (x:xs) is not a tuple. It is a list by definition.

On Wed, Feb 24, 2016 at 2:45 AM Imants Cekusins <[hidden email]> wrote:
Hello Xiao,

(one_element)             -- is evaluation
(element,element,...)    -- is tuple

(1:[2])     -- [1,2] because it is one "array element"
(1,2)      -- is a tuple because there are 2 elements
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Graham Gill
In reply to this post by Nan Xiao
Hi Nan, are you just confused about the use of the parentheses "(" and ")"?

(x1,x2), (x1,x2,x3), ... are tuples in Haskell, but (x:xs) is not.
(There's no "one-ple", or 1-tuple, in Haskell.) In

occurs value [] = 0
occurs value (x:xs) = (if value == x then 1 else 0) + occurs value xs

the "(" and ")" around "x:xs" are just there for grouping, for operator
precedence reasons. Function application binds more tightly than ":". If
you leave the parentheses off, such as in

occurs value x:xs = ...

you'll get a parse error.

Graham


On 2/24/2016 5:31 AM, Nan Xiao wrote:

> Hi all,
>
> Greetings from me!
>
> I am confused about the function parameters and tuple. E.g.:
>
> occurs value [] = 0
> occurs value (x:xs) = (if value == x then 1 else 0) + occurs value xs
>
> should we consider (x:xs) as a tuple?
>
> Thanks in advance!
>
> Best Regards
> Nan Xiao
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Nan Xiao
Hi all,

Firstly, thanks very much for all responses!

Rein referred "A tuple can have any number of elements", while Graham
referred "There's no "one-ple", or 1-tuple, in Haskell.". So which one
is right? The tuple at least contains 2 elements?

Thanks very much in advance!
Best Regards
Nan Xiao


On Thu, Feb 25, 2016 at 2:37 AM, Graham Gill <[hidden email]> wrote:

> Hi Nan, are you just confused about the use of the parentheses "(" and ")"?
>
> (x1,x2), (x1,x2,x3), ... are tuples in Haskell, but (x:xs) is not. (There's
> no "one-ple", or 1-tuple, in Haskell.) In
>
> occurs value [] = 0
> occurs value (x:xs) = (if value == x then 1 else 0) + occurs value xs
>
> the "(" and ")" around "x:xs" are just there for grouping, for operator
> precedence reasons. Function application binds more tightly than ":". If you
> leave the parentheses off, such as in
>
> occurs value x:xs = ...
>
> you'll get a parse error.
>
> Graham
>
>
>
> On 2/24/2016 5:31 AM, Nan Xiao wrote:
>>
>> Hi all,
>>
>> Greetings from me!
>>
>> I am confused about the function parameters and tuple. E.g.:
>>
>> occurs value [] = 0
>> occurs value (x:xs) = (if value == x then 1 else 0) + occurs value xs
>>
>> should we consider (x:xs) as a tuple?
>>
>> Thanks in advance!
>>
>> Best Regards
>> Nan Xiao
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Imants Cekusins

My guess is: tuple must contain 2+ elements.

Try to enter (1) in ghci. It is displayed as 1

Parentheses are only recognized as a tuple if there are elements separated by a comma. Otherwise an expression is assumed.


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Josh Barney
The way I learned it:
A tuple type is defined by the types inside it, so an (int,int) is not the same type as an (int, maybe(int)). With a one-tuple the type would just be (int) and that provides zero benefit. 
Instead of trying to do `first (...my expression...)` to get the first element of the one tuple, it just becomes the value inside.

Sent from my iPhone

On Feb 24, 2016, at 7:02 PM, Imants Cekusins <[hidden email]> wrote:

My guess is: tuple must contain 2+ elements.

Try to enter (1) in ghci. It is displayed as 1

Parentheses are only recognized as a tuple if there are elements separated by a comma. Otherwise an expression is assumed.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Max Voit-2
In reply to this post by Nan Xiao
On Thu, 25 Feb 2016 07:49:56 +0800
Nan Xiao <[hidden email]> wrote:

> Rein referred "A tuple can have any number of elements", while Graham
> referred "There's no "one-ple", or 1-tuple, in Haskell.". So which one
> is right? The tuple at least contains 2 elements?

There is no one-tuple; however, there's a zero-tuple. Also no arbitrary
number of tuple elements allowed, due to definition (we're at 61 for
some reason). Refer to

http://hackage.haskell.org/package/ghc-prim-0.4.0.0/docs/GHC-Tuple.html

best, Max
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: The (x:xs) in function parameter is a tuple?

Rein Henrichs
Nan, generally speaking (e.g., in maths) tuples can contain any number of elements. Haskell specifically disallows one-tuples because of the ambiguity in parsing them. Instead, (expr) is just evaluated as expr. On the other hand, it does allow (), which is a zero-tuple. Haskell's compilers also impose an upper bound on the practical size of a tuple (for modern GHC, that's 61, as Max mentioned) but that isn't a restriction of the Haskell language itself. If you want, you can consider Identity a to be a one-tuple, but it obviously doesn't use tuple syntax.

As a side note, a 1-tuple is occasionally called a monad in other disciplines, while a monad is actually defined as a triple (a three-tuple) in maths. None of that is relevant to Haskell though.

On Thu, Feb 25, 2016 at 7:14 AM Max Voit <[hidden email]> wrote:
On Thu, 25 Feb 2016 07:49:56 +0800
Nan Xiao <[hidden email]> wrote:

> Rein referred "A tuple can have any number of elements", while Graham
> referred "There's no "one-ple", or 1-tuple, in Haskell.". So which one
> is right? The tuple at least contains 2 elements?

There is no one-tuple; however, there's a zero-tuple. Also no arbitrary
number of tuple elements allowed, due to definition (we're at 61 for
some reason). Refer to

http://hackage.haskell.org/package/ghc-prim-0.4.0.0/docs/GHC-Tuple.html

best, Max
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners