Skip to content

Instantly share code, notes, and snippets.

View tafsiri's full-sized avatar

Yannick Assogba tafsiri

View GitHub Profile
//Pinstats.
//Fetch the pinterest front page. Extract all the name elements
//and keep track of the gender of what you find
package main
import (
"code.google.com/p/go-html-transform/html/transform"
"fmt"
module Main where
-- Q1. Write a function that looks up a hash table value that uses the Maybe Monad. (IMHO this
-- question is kinda sloppily written, does the hash table use the maybe monad or the function?).
-- Write a hash that stores other hashes, several levels deep. Use the Maybe monad to retrieve
-- an element several levels deep. (wtf does this even mean? is the maybe monad going to do the
-- retrieving for me?)
-- What I am going to attempt to do (my interpretation of the above):
-- 1. Write a function that looks up a value by key (in a collection of key value pairs)
-- and returns a result wrapped in the Maybe monad.
-- Q4 Write a function that takes an argument x and returns a lazy sequence that has
-- every third number, starting with x. Then write a second function that includes every fifth
-- number beginning with y. Combine these functions through composition to return every eighth
-- number beginning with x + y.
-- sooooo... the first solution to the first two was kind of given in the book (the myrange
-- function) and i don't fully get the third part. From what i understand about function composition
-- the return type of first function must match the argument type of the second function in the chain
-- this makes complete sense; if f . g = \x -> f (g x), the (g x) must result in something f can take
-- in this case both functions takes a number but returns a sequence. This sequence cannot be an
-- Q3 Write a function that converts a string to a numerical type. The string is in the form
-- $2,345,678.99 and can have leading zeroes.
-- I'm not going to be doing error checking on the format btw
strToNum :: Fractional a => [Char] -> a
strToNum str = let
-- remove the '$' and ','
plain = filter (\x -> x /= '$' && x /= ',') str
whole = takeWhile (\x -> x /= '.') plain
-- Q1 Write a function to sort a list
--Trying out bubble sort just to see how i would approach it in haskell
--recursve sorts like quicksort would likely be more natural to express
--helper function does one iteration of the bsort algorithm
bsorthelper [] = []
bsorthelper [a] = [a]
bsorthelper lst = let
(f:s:rest) = lst
module Main where
-- Q1. How many different ways can you write the function allEven?
-- allEven using built in higher order functions
ae1 :: [Integer] -> Bool
ae1 x = all even x
-- allEven with recursion
ae2 [] = True
ae2 (h:t) = if even h then ae2 t else False
(ns day-3
(:use clojure.contrib.pprint))
;Q1 use refs to create a vector of accounts in memory. Create debit and credit
; functions to change the balance of an account
; Note: I'm going to use a map instead of a vector so that accounts can have names
(def bank (ref { }))
(defn make-account [bank account-name balance]
;The sleeping barber problem http://en.wikipedia.org/wiki/Sleeping_barber_problem
; Q. Calculate how many haircuts can be done in 10 seconds
; One barber, one barber chair
; Three chairs in waiting room
; Customers arrive at random intervals 10-30ms apart
; Haircuts take 20ms
; (ns day-3
; (:use clojure.contrib.pprint))
;standard stack consuming fib
(defn fib [n]
(cond
(= 0 n) 0
(= 1 n) 1
:else (+ (fib (- n 1)) (fib (- n 2)))))
;tail recursive fib using loop-recur
;loop-recur allow you to rebind variables each
(ns day1.core)
(defn big [str n]
"Returns true if string [str] is longer than [n]
false otherwise"
(> (count str) n))
;usage
(big "hello" 2)