Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created November 30, 2015 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Garciat/33f1caff0b7c9d9d3220 to your computer and use it in GitHub Desktop.
Save Garciat/33f1caff0b7c9d9d3220 to your computer and use it in GitHub Desktop.
module Main
%include C "stdlib.h"
%include C "string.h"
%include C "stdio.h"
-----
malloc : Int -> IO Int
malloc = foreign FFI_C "malloc" (Int -> IO Int)
memset : Int -> Int -> Int -> IO ()
memset = foreign FFI_C "memset" (Int -> Int -> Int -> IO ())
puts : String -> IO ()
puts = foreign FFI_C "puts" (String -> IO ())
puts_ptr : Int -> IO ()
puts_ptr = foreign FFI_C "puts" (Int -> IO ())
-----
instance Enum Char where
toNat c = toNat (ord c)
fromNat n = chr (fromNat n)
pred c = fromNat (pred (toNat c))
enumerate : List a -> List (Nat, a)
enumerate xs = zip [0..length xs] xs
-----
main : IO ()
main = do p <- malloc 50
memset p 0 50
for_ (enumerate ['a'..'z']) $ \ix => do
let i = fst ix
let x = snd ix
memset (p + cast i) (cast x) 1
puts_ptr p
-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment