Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell function process

Tags:

haskell

integerList = [1] ++ integerList

(head (tail integerList))

I have run this code and the result is 1 and that it is an endless recursion. I am trying to figure out how haskell calculates these functions. Can someone write down the process. I would like to visualize it. Thanks!

like image 500
DoubleOseven Avatar asked Mar 05 '26 05:03

DoubleOseven


1 Answers

head returns the first element of a list. In this case it returns the first element of the list tail integerList. tail returns the original list without the first element. The original list is integerList which is bound to [1] ++ integerList. The ++ operator concatenates two lists, so the resulting list is 1 : integerList. Applying tail to this list gives integerList, so tail integerList simply yields integerList.

Back to the beginning: substitute tail integerList with integerList (since that's what it evaluates to) to get head integerList. Reminder: integerList evaluates to 1 : integerList. Applying head to that we get 1.

like image 52
d3dave Avatar answered Mar 06 '26 21:03

d3dave