haskell

Haskell - Calculating the shortest path using trees

旧街凉风 提交于 2020-01-01 07:11:09
问题 i am trying to write a code in haskell, that goes from point A, to point F, on a board game, that is essentially a Matrix, following the shortest path. This is the board: AAAA ACCB ADEF * 0 0 N The robot enters on the letter A, on the bottom (where it is the * ), and must reach F, on the bottom of the board are the coordinates, x=0, y=0, and pointing towards North. F coordinate is (3,0) The trick is, it can't jump more than one letter, it can go from A to B, B to C, etc. and it can walk

Efficient Haskell equivalent to NumPy's argsort

亡梦爱人 提交于 2020-01-01 06:05:51
问题 Is there a standard Haskell equivalent to NumPy's argsort function? I'm using HMatrix and, so, would like a function compatible with Vector R which is an alias for Data.Vector.Storable.Vector Double . The argSort function below is the implementation I'm currently using: {-# LANGUAGE NoImplicitPrelude #-} module Main where import qualified Data.List as L import qualified Data.Vector as V import qualified Data.Vector.Storable as VS import Prelude (($), Double, IO, Int, compare, print, snd) a ::

Manipulating “arbitrary” tuples

天涯浪子 提交于 2020-01-01 05:44:13
问题 I have simple tuples (e.g. read from a DB) from that I do not know the number of elements nor the content. E.g. (String, Int, Int) or (String, Float, String, Int) . I want to write a generic function that would take all sort of tuples and replace all data with the string "NIL". If the string "NIL" is already present it should stay untouched. Coming back to the example: ("something", 3, 4.788) should result in ("something", "NIL", "NIL") ("something else", "Hello", "NIL", (4,6)) should result

Erlang's term_to_binary in Haskell?

好久不见. 提交于 2020-01-01 05:37:06
问题 Is there a no-fuss serialization method for Haskell, similar to Erlang's term_to_binary/binary_to_term calls? Data.Binary seems unnecessarily complicated and raw. See this example where you are basically manually encoding terms to integers. 回答1: Use Data.Binary, and one of the deriving scripts that come with the package. It's very simple to derive Binary instances, via the 'derive' or 'deriveM' functions provided in the tools set of Data.Binay. derive :: (Data a) => a -> String For any 'a' in

Example of how to parse exiftool JSON output in Haskell

谁都会走 提交于 2020-01-01 05:35:13
问题 I can't make sense of any of the documentation. Can someone please provide an example of how I can parse the following shortened exiftool output using the Haskell module Text.JSON ? The data is generating using the command exiftool -G -j <files.jpg> . [{ "SourceFile": "DSC00690.JPG", "ExifTool:ExifToolVersion": 7.82, "File:FileName": "DSC00690.JPG", "Composite:LightValue": 11.6 }, { "SourceFile": "DSC00693.JPG", "ExifTool:ExifToolVersion": 7.82, "File:FileName": "DSC00693.JPG", "EXIF

hsc2hs: Mutate a C struct with Haskell

荒凉一梦 提交于 2020-01-01 05:34:33
问题 I am trying to write a Haskell program that communicates with C (ultimately for iOS via GHC-iOS). I want it to pass a string from C to Haskell, have Haskell process it and then return some Data types from Haskell to C Structs via hsc2s. I have been unsuccessful at finding a clear, simple tutorial. The only thing Haskell needs from C is the String, nothing else. I have no trouble with the very first part, passing a string to Haskell. testPrint :: CString -> IO () testPrint c = do s <-

Use of 'unsafeCoerce'

感情迁移 提交于 2020-01-01 05:20:06
问题 In Haskell, there is a function called unsafeCoerce , that turns anything into any other type of thing. What exactly is this used for? Like, why we would you want to transform things into each other in such an "unsafe" way? Provide an example of a way that unsafeCoerce is actually used. A link to Hackage would help. Example code in someones question would not. 回答1: unsafeCoerce lets you convince the type system of whatever property you like. It's thus only "safe" exactly when you can be

How do I check if a simplex contains the origin?

99封情书 提交于 2020-01-01 05:03:30
问题 I am implementing the Gilbert-Johnson-Keerthi algorithm which computes whether two objects are intersecting (ie. colliding). The entry point to my code is the hasCollided function which takes two lists of points and returns True if they are intersecting. I believe I have implemented the paper correctly - however, I still have to implement the contains function. The contains function should determine whether a simplex contains the origin. I am unsure as to how to implement this. How do I

Lists of fixed length and type literals

主宰稳场 提交于 2020-01-01 04:57:31
问题 I'm trying to define a type for lists of fixed length in Haskell. When I use the standard way to encode natural numbers as types in unary, everything works fine. However, when I try to build everything on GHC's type literals, I run into tons of problems. My first shot at the desired list type was data List (n :: Nat) a where Nil :: List 0 a (:>) :: a -> List n a -> List (n+1) a which unfortunately didn't allow for writing a zip function with type zip :: List n a -> List n b -> List n (a,b) I

Implementing Iota in Haskell

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 04:56:05
问题 Iota is a ridiculously small "programming language" using only one combinator. I'm interested in understanding how it works, but it would be helpful to see the implementation in a language I'm familiar with. I found an implementation of the Iota programming language written in Scheme. I've been having a little trouble translating it to Haskell though. It's rather simple, but I'm relatively new to both Haskell and Scheme. How would you write an equivalent Iota implementation in Haskell? (let