This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def parse(expr): | |
| lst = [] | |
| for line in expr.split('\n'): | |
| s = line.split(' -> ') | |
| if len(s) == 1: | |
| lst.append(('def', line)) | |
| else: | |
| lst.append(('imply', s[0], s[1])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang racket | |
| (define (op? e) | |
| (member e '(+ *))) | |
| (define (op->procedure op) | |
| (case op | |
| [(+) +] | |
| [(*) *])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Atom = | |
| | Num of int | |
| | Op of char | |
| let opToProc = function | |
| | '+' -> (+) | |
| | '*' -> (*) | |
| | _ -> failwith "invalid operation" | |
| let rec evalRPN (expr : Atom list) (stack : int list) = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(prog). | |
| -export([fact/2, worker/3, manager/1, main/0]). | |
| fact(N, Start) -> lists:foldl(fun(L, R) -> L*R end, 1, lists:seq(Start, N)). | |
| worker({Start, End}, I, Pid) -> | |
| R = fact(End, Start), | |
| Pid ! {finished, I, R}. | |
| manager(Queue) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(prog). | |
| -export([main/0, take/2, drop/2, par/2, splitAt/2, product/1, manager/3, worker/3, fact/2]). | |
| take(0, _) -> []; | |
| take(_, []) -> []; | |
| take(N, Lst) -> | |
| [hd(Lst) | take(N-1, tl(Lst))]. | |
| drop(0, Lst) -> Lst; | |
| drop(_, []) -> []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(rpn). | |
| -export([eval/2, op/1, is_op/1, start/0]). | |
| %% I hope I never have to touch Erlang again | |
| op("+") -> fun(L,R) -> L+R end; | |
| op("*") -> fun(L,R) -> L*R end. | |
| is_op(X) -> X == "+" orelse X == "*". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import operator | |
| def do_op(op, lhs, rhs): | |
| return \ | |
| {'+': operator.add, | |
| '-': operator.sub, | |
| '*': operator.mul}[op](lhs, rhs) | |
| def rpn(lst, stack): | |
| if lst == []: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open Printf | |
| type node = | |
| | Call of string * node list | |
| | Int of int | |
| let rec toString = function | |
| | Call (fn, args) -> sprintf "(%s %s)" fn (String.concat " " (List.map toString args)) | |
| | Int i -> sprintf "%d" i | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (compose f g) | |
| (lambda (x) | |
| (f (g x)))) | |
| (define (composeN acc f n) | |
| (if (zero? n) | |
| acc | |
| (composeN (compose acc f) f (- n 1)))) | |
| (define (even? n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data Tok = Op Char | NumTok Int | |
| op '+' = (+) | |
| op '*' = (*) | |
| op _ = \_ _ -> 666 | |
| eval toks = | |
| eval' toks [] | |
| where | |
| eval' [] s = head s |