Haskell

Posted: 2016-04-03 , Modified: 2016-04-03

Tags: none

Parsec

type LeafTree = Free []

leaf = Pure
node = Free

genWord = many1 (noneOf " (),\n")

parseExpr :: Parser a -> Parser (LeafTree a)
parseExpr p = (spaces >> (p >>= (return . leaf))) <|>
  do {
    char '(';
    trees <- sepEndBy (parseExpr p) spaces;
    char ')';
    return $ node trees;
  }

parseLISP' :: Parser (LeafTree String)
parseLISP' = parseExpr genWord

parseLISP :: String -> Maybe (LeafTree String)
parseLISP = fromRight . parse parseLISP' ""

Many ways to write a for loop

  1. foldl, shuffle the arguments around to your preference. foldl::foldl :: Foldable t => (a -> b -> a) -> a -> t b -> a.

    for' :: [a] -> b -> (a -> b -> b) -> b
    for' li x0 f = foldl (flip f) x0 li
    
    --simple for loop
    for' [1,2,3] 0 (\y i -> i + y)
    
    --nested for loop
    for' [1..5] 0 (\x i ->
    	for' [1..x] i (\y j -> j+y))
    -- same as (in other languages): s=0; for x in [1..5] {for y in [1..x]{s = s+y}}; return y
  2. Use Haskell’s built-in for! forM on State:

    execState (forM_ [1..5] (\x ->
    	forM_ [1..x] (\y ->
    		modify (+y)
    	)
    )) 0

See C:\Users\Owner\Dropbox\CS\hs\learning.

Useful things