Skip to content

Instantly share code, notes, and snippets.

View scmu's full-sized avatar

Shin-Cheng Mu scmu

  • Academia Sinica
  • Taiwan
View GitHub Profile
@scmu
scmu / Nub.hs
Last active April 26, 2018 15:26
One-Liner nub
import Data.Ord
import Data.List
nubSimple :: [Int] -> [Int]
nubSimple = map head . group . sort
nubStable :: [Int] -> [Int]
nubStable = map fst . sortBy (comparing snd). map head .
groupBy ((. fst).(==).fst) . sort . flip zip [0..]
@scmu
scmu / FLOLAC02.hs
Created May 4, 2018 02:32
Exercise: histogram and graph
import Control.Arrow ((&&&))
import Data.String (unlines)
import Data.List (unfoldr,foldl')
histogram :: [Int] -> String
histogram = graph . histo
histo :: [Int] -> [Int]
histo = foldl' oplus [0,0,0,0,0,0,0,0,0,0]
where oplus xs i = zipWith ($) (replicate i id ++ [(1+)] ++ repeat id) xs
@scmu
scmu / FLOLAC03.hs
Created May 12, 2018 15:54
Goldbach's conjecture
{-
Richard Bird's quick algorithm for generating prime numbers.
Note that the usual "one-liner" Haskell definition of the
sieve is not an honest implementation of the sieve of Eratosthenes.
The following algorithm by Bird is a more faithful implementation
that is not asymptotically the best, but practically
fast enough. Further more, we can reuse the "union" function.
For more info see: Melissa E. O’Neill, The genuine sieve of
{-
A program searching for solutions of a board puzzle
given by a friend.
The aim is to fill in a 8 by 8 square using the
given 8 pieces. Each piece has a particular shape and
can be rotated and flipped.
I am not satisfied with this program yet. The program