O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Create an empty list:
        emptyList = []
  1. Prepend an element to the list:
        prepend = 10 : []
  1. Create a list of five integers:
        list5 = 1 : 2 : 3 : 4 : 5 : []
  1. Create a list of integers from 1 to 10:
        list10 = [1..10]
  1. Create an infinite list:
        infiniteList = [1..]
  1. This is the head of a list:
        getHead = head [1..10]
  1. This is the tail of a list:
        getTail = tail [1..10]
  1. This is all but the last element:
        allbutlast = init [1..10]
  1. Take 10 elements:
        take10 = take 10 [1..]
  1. Drop 10 elements:
        drop10 = drop 10 [1..20]
  1. Get nth element:
        get1331th = [1..] !! 1331
  1. Check if a value is the element of the list.
        is10element = elem 10 [1..10]
  1. Do pattern matching on the list. Here we check whether the list is empty or not: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required