Skip to content

Instantly share code, notes, and snippets.

View cacharle's full-sized avatar

Charles Cabergs cacharle

View GitHub Profile
digits :: Int -> [Int]
digits x = reverse $ revDigits x
revDigits :: Int -> [Int]
revDigits 0 = []
revDigits x = x `mod` 10 : revDigits (x `div` 10)
-- check the multiplicative persistence of a given number
-- https://www.youtube.com/watch?v=Wim9WJeDTHQ&list=PLt5AfwLFPxWKuRpivZd_ivR2EvEzKrDUu&index=2
-- https://oeis.org/A003001
import System.Environment(getArgs)
import Data.List(intercalate)
main = do
args <- getArgs
let n = read (head args) :: Integer
@cacharle
cacharle / split.hs
Created August 19, 2019 16:25 — forked from ifukazoo/split.hs
split
split :: Eq a => a -> [a] -> [[a]]
split _ [] = [[]]
split delim str =
let (before, remainder) = span (/= delim) str
in before:case remainder of
[] -> []
x -> split delim $ tail x
@cacharle
cacharle / sdl_boilerplate.c
Created August 27, 2019 15:34
SDL boilerplate files for basic application
#include <stdbool.h>
#include <SDL2/SDL.h>
#define WINDOW_TITLE "Title"
#define WINDOW_X 20
#define WINDOW_Y 20
static void update(GState *state);
static void event_handler(GState *state);
static void destroy_state(GState *state);
@cacharle
cacharle / main.c
Last active September 11, 2023 13:15
SDL boilerplate files for basic application
#include "sdl_boilerplate.h"
int main(void)
{
GState *gstate = graphics_init(400, 400);
graphics_run(gstate);
graphics_quit(gstate);
return 0;
}
NAME = name
CC = gcc
CCFLAGS = -Wall -Wextra
LDFLAGS =
HEADER = header.h
SRC = main.c
OBJ = $(SRC:.c=.o)
RM = rm -f
#include <stdio.h>
union Color
{
int hexcode;
struct
{
unsigned char b;
unsigned char g;
unsigned char r;
isPrime :: Int -> Bool
isPrime 2 = True
isPrime 3 = True
isPrime x
| x < 2 = False
| x `mod` 2 == 0 || x `mod` 3 == 0 = False
| otherwise = divCheck 5
where divCheck d
| d * d > x = True
| x `mod` d == 0 || x `mod` (d + 2) == 0 = False
-- Reverse Polish Notation parser
-- from: http://learnyouahaskell.com/functionally-solving-problems
rpn :: String -> Float
rpn expr = head $ foldl foldFunc [] (words expr)
where foldFunc acc op
| head op `elem` ['1'..'9'] = (read op :: Float) : acc
| otherwise = applyOp op (acc !! 1) (head acc) : drop 2 acc
applyOp op a b = case op of "+" -> a + b
"-" -> a - b
@cacharle
cacharle / formated.txt
Created November 17, 2019 22:22
cheatsheet to read files in c
nom=didier age=43,xp=65,hp=100
nom=dupond age=12,xp=34,hp=110
nom=jean age=1,xp=2,hp=110