Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
APL
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Reference implementation = This implementation of the above API is intended to give its semantics. It is not intended to run fast! <haskell> data Array a = Arr Shape [a] type Shape = [Nat] -- Invariant: Arr s xs: product s = length xs ravel :: Array a -> Vector a ravel (Arr s a) = Arr [product s] a zilde :: Vector a -- Empty vector, rank 1 zilde = Arr [0] [] enclose :: a -> Scalar a -- Returns a rank 0 array, of -- depth one greater than input enclose x = Arr [] [x] disclose :: Scalar a -> a disclose (Arr [] [x]) = x discloseA (Arr outer items) = Arr (outer ++ inner) [ i | Arr _ is <- items, i <- is ] where (Arr inner _ : _) = items transpose :: Vector Nat -- Permutation of (iota (rank arg)) -> Array a -- Arg -> Array a transpose (Arr [n] perm) (Arr shape items) = assert (n == length shape ) $ Arr (permute perm shape) (scramble ... items) -- Property: disclose (enclose x) == x shape :: Array a -> Vector Nat shape (Arr s a) = Arr [1] s reshape :: Vector Nat -- s: the shape -> Array a -- Arbitrary shape -> Array a -- Shape of result = s reshape (Arr [n] s) (Arr s' elts) | null elts = error "Reshape on empty array" | otherwise = Arr s (take (product s) (cycle elts)) each :: (a -> b) -> Array a -> Array b each f (Arr s xs) = Arr s (map f xs) reduce :: (Array a -> Array a -> Array a) -> Array a -- All rank one smaller than input -> Array a -- Input -> Array a -- Rank one smaller than input -- except that rank 0 input gives identity reduce k z (Arr [] [item]) = Arr [] [item] -- Identity on rank 0 reduce k z (Arr (s:ss) items) = foldr k z (chop ss items) encloseA :: Nat -> Array a -> Array a encloseA n (Arr shape items) = Arr outer_shape (chop inner_shape items) where (outer_shape, inner_shape) = splitAt n shape chop :: Shape -> [item] -> [Array item] chop s [] = [] chop s is = Arr s i : chop s is' where (i,is') = splitAt (product s) is </haskell> == A couple of examples == <tt> x = 0 1 2 : Array Float = Arr [2,3] [0,1,2,3,4,5] 3 4 5 enclose x :: Array (Array Float) = Arr [] [Arr [2,3] [0,1,2,3,4,5]] </tt>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width