Skip to content

Instantly share code, notes, and snippets.

import Data.List (sortOn)
reorder :: [Char] -> [Int] -> [Char]
reorder a b = map fst . sortOn snd $ zip a b
a = ['C'..'H']
b = [3, 0, 4, 1, 2, 5]
main = putStrLn $ reorder a b
#!/bin/bash
tail -n+3 "$0" | gcc -xc - && ./a.out && rm ./a.out ; exit
//
#include <stdio.h>
int main() {
printf("C is a scripting language!\n");
}
Prelude Data.List> numChars s c = foldr (\x acc -> if x == c then acc+1 else acc) 0 s
Prelude Data.List> numChars "oh heavens" 'h'
2
*Main> moveZeros [1, 2, 0, 1, 0, 0, 3, 6]
[1,2,1,3,6,0,0,0]
import Data.List
import Data.Ord
charNumSort :: [String] -> [String]
charNumSort = sortBy ((comparing $ length . nub) `mappend` (comparing $ Down . length))
/tmp
➜ ./a.out
0
1
2
1
8
1
32
/tmp
➜ ./a.out
0
1
0
1
0
1
*Main> oneRow ["candy","doodle","pop","shield","lag","typewriter"]
["pop","lag","typewriter"]
*Main> maxPointsOnLine [(1,1), (3,2), (5,3), (4,1), (2,3), (1,4)]
4
import Control.Monad
generateParens :: Int -> [String]
generateParens n = generateParens' n ""
generateParens' :: Int -> String -> [String]
generateParens' n xs
| n == o = [xs ++ replicate u ')']
| u == 0 = generateParens' n (xs ++ "(")
| otherwise = [xs ++ "(", xs ++ ")"] >>= generateParens' n