Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the double-backslash \\ mean in Haskell?

Tags:

syntax

haskell

I'm trying to figure out what \\ means in Haskell?

I've got a program that runs perfectly when compiled, implying that \\ is a built-in function, since it's not defined anywhere in the program. However, in GHCi, if I try to use it in the same manner, I get an error saying it's not in the scope.

Does it mean something special in each context?

Here is my code;

module Main where

import Data.List

numbersA = [1, 105, 103, 7, 4, 102, 3, 101, 107, 8, 9]

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

type Number = Integer

type Run = [Number]

extractRuns :: [Number] -> [Run]
extractRuns [] = []
extractRuns xs = run : extractRuns xs'
                   where run = buildRun (head xs) (tail xs)
                         xs' = xs \\ run

buildRun :: Number -> [Number] -> Run
buildRun seed numbers
  | endRun    = [seed]

  | otherwise = seed : buildRun seed' numbers'
                  where endRun     = successors == []
                        successors = filter (.#. seed) numbers
                        numbers'   = numbers \\ [seed]
                        seed'      = head successors

(.#.) :: Number -> Number -> Bool
x .#. y = x /= y && abs (x - y) < 10

runs = extractRuns numbersA

main = print runs
like image 265
CodyBugstein Avatar asked Feb 12 '13 04:02

CodyBugstein


1 Answers

\\ is the list difference operator. You can use it in GHCi you just have to import Data.List first.

Prelude> :m Data.List
Prelude Data.List> [1..5]\\[3,4]
  [1,2,5]

On a sidenote, this is why I'd suggest avoiding mass imports like import Data.List and to be more specific import Data.List ( (\\) ) so you can be more aware of just what you're importing.

like image 179
Daniel Gratzer Avatar answered Sep 30 '22 16:09

Daniel Gratzer