haskell

List of different types?

被刻印的时光 ゝ 提交于 2019-12-27 17:29:13
问题 data Plane = Plane { point :: Point, normal :: Vector Double } data Sphere = Sphere { center :: Point, radius :: Double } class Shape s where intersect :: s -> Ray -> Maybe Point surfaceNormal :: s -> Point -> Vector Double I have also made both Plane and Sphere instances of Shape . I'm trying to store spheres and planes in the same list, but it doesn't work. I understand that it shouldn't work because Sphere and Plane are two different types, but they are both instances of Shape , so shouldn

Cabal not installing dependencies when needing profiling libraries?

拟墨画扇 提交于 2019-12-27 17:02:56
问题 I want to compile my program with profiling, so I run: $ cabal configure --enable-executable-profiling ... $ cabal build ... Could not find module 'Graphics.UI.GLUT': Perhaps you havent installed the profiling libraries for package 'GLUT-2.2.2.0'? ... $ # indeed I have not installed the prof libs for GLUT, so.. $ cabal install -p GLUT --reinstall ... Could not find module 'Graphics.Rendering.OpenGL': Perhaps you havent installed the profiling libraries for package 'OpenGL-2.4.0.1'? ... So,

How do you represent a graph in Haskell?

烂漫一生 提交于 2019-12-27 12:42:29
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current

How do you represent a graph in Haskell?

帅比萌擦擦* 提交于 2019-12-27 12:40:10
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current

How do you represent a graph in Haskell?

旧巷老猫 提交于 2019-12-27 12:39:50
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current

What is the purpose of Rank2Types?

依然范特西╮ 提交于 2019-12-27 12:07:37
问题 I am not really proficient in Haskell, so this might be a very easy question. What language limitation do Rank2Types solve? Don't functions in Haskell already support polymorphic arguments? 回答1: Do not functions in Haskell already support polymorphic arguments? They do, but only of rank 1. This means that while you can write a function that takes different types of arguments without this extension, you can't write a function that uses its argument as different types in the same invocation.

Haskell types frustrating a simple 'average' function

╄→гoц情女王★ 提交于 2019-12-27 11:53:13
问题 I'm playing around with beginner Haskell, and I wanted to write an average function. It seemed like the simplest thing in the world, right? Wrong. It seems like Haskell's type system forbids average from working on a generic numeric type - I can get it to work on a list of Integrals, or an list of Fractionals, but not both. I want: average :: (Num a, Fractional b) => [a] -> b average xs = ... But I can only get: averageInt :: (Integral a, Fractional b) => [a] -> b averageInt xs = fromIntegral

How to create a polyvariadic haskell function?

妖精的绣舞 提交于 2019-12-27 11:08:27
问题 I need a function which takes an arbitrary number of arguments (All of the same type), does something with them and afterwards gives a result back. A list of arguments is impracticable in my specific case. As I looked through the haskell libs, I saw that the function printf (from module Text.Printf ) uses a similar trick. Unfortunately, I couldn't understand that magic by looking at the source. Can somebody explain how to achieve this, or at least some webpage/paper/whatever where I could

Defining a function by equations with different number of arguments

冷暖自知 提交于 2019-12-27 10:39:47
问题 I noticed today that such a definition safeDivide x 0 = x safeDivide = (/) is not possible. I am just curious what the (good) reason behind this is. There must be a very good one (it's Haskell after all :)). Note: I am not looking suggestions for alternative implementations to the code above, it's a simple example to demonstrate my point. 回答1: I think it's mainly for consistency so that all clauses can be read in the same manner, so to speak; i.e. every RHS is at the same position in the type

Haskell Zip Parse Error

时间秒杀一切 提交于 2019-12-27 04:48:31
问题 I am trying to use the zip function in Haskell to join two lists together. The lists could be defined and info gathered as follows: priority <- getLine let priorityList = [] priority : priorityList name<- getLine let nameList = [] name : nameList After gathering the info, the expected output would be priorityList = [1,2,3] & nameList = [test1, test2, test3]. However, this is unimportant for the purpose of the question, it can be assumed that the two lists are in the following format: