Skip to content

Instantly share code, notes, and snippets.

View tautologico's full-sized avatar

Andrei Formiga tautologico

  • UFPB
  • João Pessoa, Paraíba
View GitHub Profile
@tautologico
tautologico / letter_cover.ml
Last active August 29, 2015 14:10
Finding the number of different letters in a string, ignoring spaces
let string_fold s f ini = (* this is a left fold *)
let len = String.length s in
let rec loop i res =
if i >= len then res else loop (i+1) (f s.[i] res)
in
loop 0 ini
let explode s = (* a right string fold wouldn't need the reverse *)
List.rev @@ string_fold s (fun c l -> c :: l) []
@tautologico
tautologico / SomaPares.java
Created April 17, 2011 22:47
Exemplo de como gerar uma lista que contem a soma dos pares em outra lista em Java
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class SomaPares {
public static List<Integer> somapares(List<Integer[]> pares)
{
List<Integer> result = null;
if (pares != null)
{
@tautologico
tautologico / somapares.ml
Created April 17, 2011 22:48
Exemplo de como criar uma lista que contem a soma dos pares em outra lista em OCaml
let rec somapares l =
match l with
[] -> []
| (a, b) :: rl -> (a+b) :: somapares rl
somapares [(1, 5); (4, 7)]
let somapares l = List.map (fun (a,b) -> a+b) l in
somapares [(1, 5); (4,7)]
@tautologico
tautologico / gist:925104
Created April 18, 2011 10:06
QuickSort in OCaml
let rec quick l =
match l with
[] -> []
| [x] -> l
| p :: rl -> (match List.partition (fun x -> x < p) rl with
(l1, l2) -> (quick l1) @ [p] @ (quick l2))
@tautologico
tautologico / sat.hs
Created April 28, 2011 01:04
Calculating truth tables for propositional formulas
import Data.List ( nub )
import Data.Maybe ( fromJust )
data Formula = Var String | And Formula Formula |
Or Formula Formula | Not Formula
getVarsDup (Var v) = [v]
getVarsDup (And f1 f2) = (getVarsDup f1) ++ (getVarsDup f2)
getVarsDup (Or f1 f2) = (getVarsDup f1) ++ (getVarsDup f2)
getVarsDup (Not f) = getVarsDup f
@tautologico
tautologico / gist:949169
Created April 29, 2011 22:42
Split a string into a list of string by breaking "words" separated by spaces
let split s =
let l = String.length s in
let rec whitespace i =
if i >= l then [] else
if s.[i] = ' ' then whitespace (i+1) else nonws i 1
and nonws i j =
if (i+j) = l then [(i,j)] else
if s.[i+j] = ' ' then (i,j) :: whitespace (i+j) else nonws i (j+1) in
List.map (fun (i, l) -> String.sub s i l) (whitespace 0)
@tautologico
tautologico / gist:1006493
Created June 3, 2011 15:10
CamlP4 AST for a line of code
Ast.StExp (_loc,
(Ast.ExLet (_loc, Ast.ReNil,
(Ast.BiEq (_loc, (Ast.PaId (_loc, (Ast.IdLid (_loc, "f")))),
(Ast.ExFun (_loc,
(Ast.McArr (_loc, (Ast.PaId (_loc, (Ast.IdLid (_loc, "x")))),
(Ast.ExNil _loc),
(Ast.ExApp (_loc,
(Ast.ExApp (_loc,
(Ast.ExId (_loc, (Ast.IdLid (_loc, "*")))),
(Ast.ExId (_loc, (Ast.IdLid (_loc, "x")))))),
@tautologico
tautologico / gist:1048893
Created June 27, 2011 13:53
Imprimindo árvores binárias em Prolog
print_tree(void).
print_tree(t(X, void, void)) :- write(X), !.
print_tree(t(X, L, R)) :- write('('), write(X), write(' '), print_tree(L), write(' '), print_tree(R), write(')').
@tautologico
tautologico / coquetel.pl
Created June 28, 2011 14:54
Resolução de um desafio de lógica em Prolog
/* p(Nome, Sobrenome, Brincadeira, Vitima) */
gera(p(N, S, B, V)) :-
member(N, [ana, ester, pablo, rodolfo]),
member(S, [fontes, levis, matoso, salgado]),
member(B, [almofada, aranha, foto, mosca]),
member(V, [irmao, mae, pai, tia]).
dif(p(N1, S1, B1, V1), p(N2, S2, B2, V2)) :-
N1 \= N2, S1 \= S2, B1 \= B2, V1 \= V2.