Skip to content

Instantly share code, notes, and snippets.

@roymath-zz
Last active July 20, 2018 15:53
Show Gist options
  • Save roymath-zz/66046ba6573a126229ff to your computer and use it in GitHub Desktop.
Save roymath-zz/66046ba6573a126229ff to your computer and use it in GitHub Desktop.
1. Figure out if all values in a list are True
areTrue :: [Bool] -> Bool
areTrue xs = (length [x|x<-xs, x==True]) == length xs
2. Concatenate a list of lists.
concat :: [[a]] -> [a]
concat xss = [x | xs<-xss, x<-xs]
Main.concat [[1,3], [2,4]]
3. Produce a list with n identical elements
ident :: (Num n, Enum n) => n -> a -> [a]
ident n x = [x | _ <- [1..n]]
ident 3 True
4. Select the nth element of a list
nSel :: (Num a, Num n, Eq n, Enum n) => [a] -> n -> a
nSel xs n = head [x | (x, i) <- zip xs [0..], i==n]
nSel [1,4,0] 1
5. Decide if an element is present in a list
isElem :: (Eq a) => [a] -> a -> Bool
isElem xs x = length([e | e <- xs, e==x]) >= 1
isElem [1, 3, 5, 1] 1
6. Other stuff goes here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment