Posts

Showing posts with the label toy

Unary math Towers of Hanoi in naked (D)ASH and BASH

The following solution assumes neither BASH extensions, nor any external executables. Hence it could be ran from a preboot environment like a frugal initrd. A slight compromise was to use unary math. Did you know, that you can access an animated version from Emacs by typing M-x hanoi ? pop(){  shift  echo $* } top(){  echo $1 } print_towers(){  echo "a=[" $a "], b=[" $b "], c=[" $c "]" } hanoi(){  [ "$n" ] || return  local ln  ln=$(pop $n)  n=$ln  hanoi $1 $3 $2  print_towers  echo "$1 -> $2"  eval $2=\"$(eval top \$$1) $(echo \$$2)\"  eval $1=\"$(eval pop \$$1)\"  n=$ln  hanoi $3 $2 $1 } main(){  a="1 2 3 4 5"  b=  c=  n=$a  hanoi a b c  print_towers } main "$@"

Simple Pascal-triangle implementations

-- The first version can output a row of Pascal's triangle : import System.Environment(getArgs) pas 0       = [1] pas n | n>0 = let l = pas (n-1) in zipWith (+) (0:l) (l++[0]) main = do     [s] <- getArgs     print . pas $ read s -- The second revision can output a certain number of rows of the triangle: import System.Environment(getArgs) pas = iterate nextline [1] where     nextline l = zipWith (+) (0:l) (l++[0]) main = do     [s] <- getArgs     putStr . unlines . map show $ take (read s+1) pas -- And the last monolith solves a very simple homework assignment: import Data.List(intersperse) import System.Environment(getArgs) pas 0 = [1] pas n = let l = pas (n-1) in zipWith (+) (0:l) (l++[0]) strPas s = do     let n = read s     if n<0 || n>30 then         fail ...

Playing with lambda elimination and point-free style

It was a long time since I've last played with Haskell, so I decided to do a bit of lambda elimination as an exercise. let f=(\x y->x++y);g=(\x y z->x++y++z)in f(g "1""2""3")"4" let f=(++);g=(f.).f in f(g "1""2""3")"4" let f=(++);g=(f.).f in (f(g "1""2""3"))"4" let f=(++);g=(f.).f in (f.(g "1""2"))"3""4" let f=(++);g=(f.).f in (f.((g "1")"2"))"3""4" let f=(++);g=(f.).f in ((f.).(g "1"))"2""3""4" let f=(++);g=(f.).f in ((f.).g "1")"2""3""4" let f=(++);g=(f.).f in (((f.).).g)"1""2""3""4" let f=(++);g=(f.).f;h=((f.).).g in h"1""2""3""4" let f=(++);h=((f.).).(f.).f in h"1""2""3""4" let ...

Chopping a long string into smaller ones toy

I made a great tool I can't do without from now on! ;-D Update#1: added null string checking, renamed "i" to "s". Update#2: upgraded output format, added BASH version! Here's the Haskell source code: import System.Environment(getArgs) import Data.List(unfoldr,intersperse) chop k | k>0 = unfoldr f where  f [] = Nothing  f s  = Just $ splitAt k s main = do  [k,s@(_:_)] <- getArgs  let out = show . concat . intersperse " "      res = chop (read k) s  putStrLn $ "filter(/=' ')" ++ out res Typing: ./chop.hs 27 http://bkil.blogspot.com/2009/05/chopping-long-string-into-smaller-ones.html Gives: filter(/=' ')"http://bkil.blogspot.com/20 09/05/chopping-long-string- into-smaller-ones.html" Or if you prefer, you could alternatively opt for the following BASH routine: main(){  printf 'echo "'  printf "$2"|sed -r "s~.{$1}~& ~g"  echo '"|sed "s~ ~~g"' } This one...

Quine in Erlang

How could I have forgotten about the friend of mine who's fond of Erlang? :-D -module(q). -export([s/0]). s()->io:format("~s~p.\n",[p(),p()]). p()->"-module(q).\n-export([s/0]).\n\ns()->io:format(\"~s~p.\\n\",[p(),p()]).\n\np()->". Here's another one for the Eshell REPL: fun(P)->io:format("~s\n~p).\n",[P,P])end( "fun(P)->io:format(\"~s\\n~p).\\n\",[P,P])end("). And as I know he's also in love with LISP, here's a Haskell variant rewritten in that spirit: (\ (x)-> ((++) x (show x)))"(\\ (x)-> ((++) x (show x)))"

Quine in Haskell

I've constructed a neat little quine in Haskell, the popular research language we all adore. ;-) It's so short, I could have pasted it right into the title! :-D putStr(p++show p)where p="putStr(p++show p)where p=" That's what I like about Haskell: the solutions you write in it are usually elegant, conscious and close to the problem space in representation. You read the above source as follows: print out the program accompanied by a quoted (shown) version of the same, where the program is just what I said. Can you put it any simpler than that?! I've first started on a traditional route by introducing constants for backslash, quotation mark and all that, until I've realized how foolish I was. As you may have already known, Haskell has a handy function called 'show' that does all quoting for you. I've been using that all over the place if you have read any of my sources, so I'm not sure why it slipped my mind. Sometimes we overlook the most o...

Binary numbers toy in Erlang GS graphics

Because of popular demand, I have ported the previous example to Erlang using it's built-in GS package. Enjoy! :-D All code and text in this post is Copyright (c) bkil.hu [also known as bkil], 2009; and is licensed under the GNU GPL v2. Refer to the standard license texts from June 1991 for exact conditions. Here is an example link: http://www.gnu.org/licenses/gpl-2.0.html Here's the core (simbins.erl): -module(simbins). -export([radix/3, binaries/2, simbins/1]). %% Radix conversion with LSB output. radix(_,0,_) -> []; radix(R,W,N) -> [N rem R | radix(R, (W-1), (N div R))]. %% A full-matrix transpose stub. transpose([H|T]) when is_list(H) -> Append = fun(V, M) -> lists:zipwith(fun(E,L) -> [E|L] end, V, M) end, Vector = fun(L) -> lists:map(fun(E) -> [E] end, L) end, [RH|RT] = lists:reverse([H|T]), lists:foldl(Append, Vector(RH), RT). %% 'binaries' is a solution to Paul R. Potts's idea. %% Prepend `lists:reverse( ` to get MSB. bi...

Binary numbers toy in Haskell SOE graphics

Image
I've been browsing Mr. Paul R. Potts's blog when I stumbled across a simple, yet neat idea. I was not quite satisfied with the lengthy solution given, so I decided to construct something similar looking from scratch. I'm using the Hugs and GHC built-in HGL/Graphics.SOE package. See the result below. Expect to see more later. All code, text and the 5 images in this post are Copyright (c) bkil.hu [also known as bkil], 2009. I place the images in the public domain. The code and text are licensed under the GNU GPL v2. Refer to the standard license texts from June 1991 for exact conditions. Here is an example link: http://www.gnu.org/licenses/gpl-2.0.html Here's the core (Simbins.hs): module Simbins where import Data.List(transpose) -- Radix conversion with LSB output. radix _ 0 _ = [] radix r w n = (n `mod` r : radix r (w-1) (n `div` r)) -- 'binaries' is a solution to Paul R. Potts's idea. -- Prepend `reverse . ` to get MSB. binaries w = transpose . map (radix ...