Haskell list basics

Hey there delilah what’s it like in new york city ……………………………… 😐

Oh sorry got lost in the music but it is really good one take time and listen it afterwards(looking for the link; like I am providing it in a tutorial! search it yourself baka :P)

So returning to our topic of haskell from last tutorial when we learnt how to write functions and basic operations and wrote few simple programs. Then I looked back on the material on haskell for some examples and got a good one to use to learn both lists and some functions, though that would be solved in next article.

First lets have a look at lists and how they are handled – >

Prelude> let list1 = [1,2,3,4,5]

Prelude> list1

[1,2,3,4,5]

Prelude> let list2 = [6,7,8,9,10]

Prelude> list1 ++ list2

[1,2,3,4,5,6,7,8,9,10]

Prelude> “concatenate strings” ++ ” ” ++ “just like that”

“concatenate strings just like that”

Prelude> ‘a’:” problem”

“a problem”

Now this is easy to see. Lists in haskell are just like other common languages – homogeneous, that is can store only one kind of elements. The keyword ‘let’ is used for defining a name right in ghci. It is not required while writing a program – take a note of this. And ‘++’ is obviously clear enough. ‘:’ operator is used to append things at the beginning. Just for the reference while concatenating haskell goes through entire list so it takes some time if the lists are large. Then notice strings are treated as list of characters. Lastly notice ‘:’ operator; it is used to append things at the beginning. In a way you can say [1,2,3] is syntactic sugar for 1:2:3:[ ]where [ ] is empty list.

So time for one example which would clear out some fogs and introduce few new keywords – reversing a list.

Code:

myreverse list = myreverse’ list [ ]

where

myreverse’ [ ] reversedlist = reversedlist

myreverse’ (x:y) reversedlist = myreverse’ y (x:reversedlist)

 

Output:

Prelude> :load reverselist.hs

# to load a script :l or :load both can be used

[1 of 1] Compiling Main ( reverselist.hs, interpreted )

Ok, modules loaded: Main.

*Main> myreverse [1,2,3,4]

[4,3,2,1]

 

This is a simple code to do so. All it does is myrevese function needs to be called and list can be passed onto it as argument. Then see ‘where’ keyword which is used to define modules or bind variables. It will be looked in detail later. Just consider it as a way to define the other function/module myreverse’ which accepts list and an empty list as arguments. Then refer to our syntactic sugar 1:2:[ ] = [1,2]. Thus, considering (x:y) is like a list [x,y]. So we bind the corresponding values in the list to x and y. Thus first element is extracted. Again using ‘:’ it is appended to the empty list. Cool right? Using this syntactic sugar of 1:2:[ ] = [1,2]?

So before ending this we end this discussion there are two things – first how we ensure argument ‘list’ is a list? That is defining type or scope. This is simple enough and will form the basis of next discussion. Secondly you could feed your hungry brains with this excellent example I found on 99 haskell practice problems – it is 36th problem in the list.

(**) Determine the prime factors of a given positive integer.

Construct a list containing the prime factors and their multiplicity.

Example in Haskell:

*Main> prime_factors_mult 315

[(3,2),(5,1),(7,1)]

 

Think about it and please try to do it yourself as I have already provided you the answer source. We will discuss it first next time and then proceed to types.

See you next time,

(and keep singing pocket no coin, soreto

you wanna be my friend?

we are, we are on the cruise! we are! …………………….. :-O can’t stop getting carried over, can you Mr. Blog owner?)

(hey and who changed the playlist?????)

 

– Payas

Advertisements

One response to “Haskell list basics

  1. Pingback: Haskell tuples | πS·

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s