Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is undefined a partial list in Haskell?

Tags:

list

haskell

Is undefined a partial list in Haskell?

I know that [1,2:undefined] is partial but what about about undefined alone?

like image 219
user2184057 Avatar asked Jun 17 '13 15:06

user2184057


3 Answers

undefined is a function that causes an error if you try to evaluate it. (However, if you don't try to evaluate it, it does no harm.) Let's examine the type signature for undefined:

ghci> :t undefined
undefined :: a

That a is a type variable, and since there are no constraints identified in the type signature (type constraints appear between :: and => symbols), a can be of any type.

I'm not sure if you really wanted a : in your example.

[1,2,undefined] is a list of integers, so the type of undefined in this expression is also an integer.

(1:2:undefined) is also a list of integers, but : takes a list as its second argument, so the type of undefined in this expression is a list of integer.

I don't think [1,2:undefined] makes sense. 1 is an integer, but 2:undefined is a list of integers. You can't have a list with elements of different types.


EDIT:

undefined isn't really a partial list*, it's just a single value (which could be of any type, including a list). For example, [1,2,undefined] is a list with three elements. The first element is 1, the second element is 2, and the third element can't be evaluated -- but it is an integer.

*However, a list with undefined as the last element could be used to represent some sort of "partial list", insofar as you can't evaluate that element. (I think that's what @Daniel means). However, if there are elements before or after it, you could evaluate them. For example:

ghci> last [1,2,undefined,4]
4

EDIT #2:

Another example might help. Here I've created a list with four elements, one of which (c) is undefined. When I query ghci to find out the type of c, I see that it's just a single integer, not a list.

ghci> let (a:b:c:d) = [1,2,undefined,4]
ghci> :type c
c :: Integer
like image 177
mhwombat Avatar answered Oct 22 '22 18:10

mhwombat


Your question is not very clear as the comments pointed out but let's make some assumptions. First you probably meant to write 1:2:undefined as your example of a partial list.

> let p1 = 1:2:undefined
> :t p1
p1 :: [Integer]

So p1 has two elements and the rest is undefined which makes it a partial list in some sense. Following this definition this type-checks:

> let p2 = undefined :: [Int]
> :t p2
p2 :: [Int]

It has 0 elements and the rest is undefined. We can call it an empty partial list.

Another way to think about it is that p1 is the same as [1,2] ++ undefined and p2 is [] ++ undefined.

like image 20
Daniel Avatar answered Oct 22 '22 19:10

Daniel


I have found answer for my question in Introduction to Functional Programing. | aka undefined is a Partial List (base of induction for them)

like image 27
user2184057 Avatar answered Oct 22 '22 18:10

user2184057