Advertisement
wtmhahagd

hint of hint of haskell

Aug 18th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Adam An
  2. -- CSCI 312
  3. -- This program demonstrates some of Haskell's list processing functions.
  4.  
  5. list = [1, 2, 3, 4, 5]
  6. main = do
  7.  
  8.   -- [1,2,3,4,5]
  9.   print list
  10.  
  11.   -- 1
  12.   print (head list)
  13.  
  14.   -- [2,3,4,5]
  15.   print (tail list)
  16.  
  17.   -- 5
  18.   print (last list)
  19.  
  20.   -- [1,2,3,4]
  21.   print (init list)
  22.  
  23.   -- 4
  24.   print (list !! 3)
  25.  
  26.   -- True (is 3 in the list?)
  27.   print (elem 3 list)
  28.  
  29.   -- 5 (size of the list)
  30.   print (length list)
  31.  
  32.   -- False (is the list empty?)
  33.   print (null list)
  34.  
  35.   -- [5,4,3,2,1]
  36.   print (reverse list)
  37.  
  38.   -- [1,2]
  39.   print (take 2 list)
  40.  
  41.   -- [3,4,5]
  42.   print (drop 2 list)
  43.  
  44.   -- 1 (least)
  45.   print (minimum list)
  46.  
  47.   -- 5 (greatest)
  48.   print (maximum list)
  49.  
  50.   -- 15 (sum)
  51.   print (sum list)
  52.  
  53.   -- 120 (product)
  54.   print (product list)
  55.  
  56.   -- [1,4,9,16,25]
  57.   print (map (\x -> x * x) list)
  58.  
  59.   -- False (is every element even?)
  60.   print (all even list)
  61.  
  62.   -- True (is at least one element odd?)
  63.   print (any odd list)
  64.  
  65.   -- [8,1,2,3,4,5] (append 8 to the front)
  66.   print (8:list)
  67.  
  68.   -- "ABCDEFGHIJKLMNOPQRSTUVWXYZ" (range)
  69.   print ['A'..'Z']
  70.  
  71.   -- [3,9,15,18,21,27,30] (comprehension on explicit list)
  72.   print [3 * x | x <- [1,3,5,6,7,9,10]]
  73.  
  74.   -- [0,0,0,0,0,0,0,0,0,0] (repeat)
  75.   print (replicate 10 0)
  76.  
  77.   -- "abc" (concatenate two lists)
  78.   print ("a"++"bc")
  79.  
  80.   -- [(1,'a'),(2,'b'),(3,'c')] (zip)
  81.   print (zip [1,2,3] ['a','b','c'])
  82.  
  83.   -- ([1,2,3],"abc") (unzip)
  84.   print (unzip [(1,'a'),(2,'b'),(3,'c')])
  85.  
  86.   -- ["Hello","world"] (split up "Hello world")
  87.   print (words "Hello world")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement