Skip to content

Instantly share code, notes, and snippets.

View Javran's full-sized avatar

Javran Cheng Javran

View GitHub Profile
@Javran
Javran / gist:8703517
Last active August 29, 2015 13:55
this code generates Pascal's triangle using a simple ball simulation: suppose there is a ball initialized at position 0, for each step, it has an equal chance of going left or right, using the "non-determinism monad"(i.e. List monad), we get all the possible positions and frequencies after n steps. Using this generated list, we are able to calcu…
import Data.List
twoWay :: Int -> [Int]
twoWay x = [x-1,x+1]
toPascalLine m = map length $ groupDup $ sort m
-- group duplicate elements together
groupDup xs = groupDupAux xs [] []
where
@Javran
Javran / gist:8716862
Created January 30, 2014 19:28
my map impl
#lang racket
(define (my-map f . lists)
(define (map-intern f xs)
(if (null? xs)
'()
(cons (f (car xs))
(map-intern f (cdr xs)))))
(if (ormap null? lists)
'()
@Javran
Javran / gist:8749140
Created February 1, 2014 07:16
reader monads are just functions.
import Control.Monad
newtype Reader e a = Reader
{ runReader :: e -> a }
instance Monad (Reader e) where
return x = Reader $ const x
m >>= f = Reader $ \e ->
runReader (f (runReader m e)) e
@Javran
Javran / gist:8787508
Created February 3, 2014 16:48
composite numbers
import Data.Numbers.Primes
seq1 = filter (not . isPrime) [2..]
-- faster
seq2 = seq2Aux primes [2..]
where
seq2Aux curP@(p:ps) curX@(x:xs)
| p < x = seq2Aux ps curX
| p > x = x: seq2Aux curP xs

我每天都在黑些啥

2014-02-04

  • 宇宙大神
  • gnome
  • ocaml
  • R
  • SICP section 4.1.6
  • "In order to understand recursion, one must first understand recursion."
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
(defun run-scheme-according-to-its-type ()
"doc todo"
(interactive)
(if (racket-file-p (buffer-file-name))
(progn
;; racket file
(run-racket-with-related-file)
(other-window 1)
(end-of-buffer)
(other-window 1))
-- if f x then Just x else Nothing
ap (<$) . (guard .)
:: (Functor f, MonadPlus f) => (a -> Bool) -> a -> f a
@Javran
Javran / gist:9181746
Last active August 29, 2015 13:56
pandoc markdown -> HTML + browser preview
(defun pandoc-markdown-to-html (file-src file-dst)
"convert markdown files into HTML files."
(shell-command
(format "pandoc %s -o %s" file-src file-dst)))
(defun current-markdown-html-preview ()
"generate HTML file for current editing file
using pandoc, and the open browser to preview
the resulting HTML file"
(interactive)