Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Adam An
- -- CSCI 312
- -- This program demonstrates some of Haskell's list processing functions.
- list = [1, 2, 3, 4, 5]
- main = do
- -- [1,2,3,4,5]
- print list
- -- 1
- print (head list)
- -- [2,3,4,5]
- print (tail list)
- -- 5
- print (last list)
- -- [1,2,3,4]
- print (init list)
- -- 4
- print (list !! 3)
- -- True (is 3 in the list?)
- print (elem 3 list)
- -- 5 (size of the list)
- print (length list)
- -- False (is the list empty?)
- print (null list)
- -- [5,4,3,2,1]
- print (reverse list)
- -- [1,2]
- print (take 2 list)
- -- [3,4,5]
- print (drop 2 list)
- -- 1 (least)
- print (minimum list)
- -- 5 (greatest)
- print (maximum list)
- -- 15 (sum)
- print (sum list)
- -- 120 (product)
- print (product list)
- -- [1,4,9,16,25]
- print (map (\x -> x * x) list)
- -- False (is every element even?)
- print (all even list)
- -- True (is at least one element odd?)
- print (any odd list)
- -- [8,1,2,3,4,5] (append 8 to the front)
- print (8:list)
- -- "ABCDEFGHIJKLMNOPQRSTUVWXYZ" (range)
- print ['A'..'Z']
- -- [3,9,15,18,21,27,30] (comprehension on explicit list)
- print [3 * x | x <- [1,3,5,6,7,9,10]]
- -- [0,0,0,0,0,0,0,0,0,0] (repeat)
- print (replicate 10 0)
- -- "abc" (concatenate two lists)
- print ("a"++"bc")
- -- [(1,'a'),(2,'b'),(3,'c')] (zip)
- print (zip [1,2,3] ['a','b','c'])
- -- ([1,2,3],"abc") (unzip)
- print (unzip [(1,'a'),(2,'b'),(3,'c')])
- -- ["Hello","world"] (split up "Hello world")
- print (words "Hello world")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement