Live data from Hacker News

Haskell tutorial for C programmers

haskell.org

1–10 of 56 posts

Re: Haskell tutorial for C programmers

#4
>What this means is that existing sort functions such as mergeSort and quickSort would need to be rewritten to sort values of the new type. In constrast, here is the type of mergeSort in Haskell mergeSort :: Ord a => [a] -> [a]

Er, you could define mergesort using templates in c++ and it would work pretty much the same.

Re: Haskell tutorial for C programmers

#5
post #4

>What this means is that existing sort functions such as mergeSort and quickSort would need to be rewritten to sort values of the new type. In constrast, here is the type of mergeSort in Haskell mergeSort :: Ord a => [a] -> [a] Er, you could define mergesort using templates in c++ and it would work pretty much the same.

How do you express the Ordinality constraint in C++?

Re: Haskell tutorial for C programmers

#6
post #4

>What this means is that existing sort functions such as mergeSort and quickSort would need to be rewritten to sort values of the new type. In constrast, here is the type of mergeSort in Haskell mergeSort :: Ord a => [a] -> [a] Er, you could define mergesort using templates in c++ and it would work pretty much the same.

How do you express the Ordinality constraint in C++?

Multiple inheritance from a purely virtual class. In Java, they're called interfaces; Comparable is the analogue to Ord.

The advantage that Haskell's typeclasses have over either Java or C++ is that the types aren't closed. I can define a new typeclass and tell the compiler that a type you defined is a member of that class at any point. In Java, if you don't define that your type is Comparable, there's nothing that I, as a consumer of your type, can do about it.

Re: Haskell tutorial for C programmers

#7
post #4

>What this means is that existing sort functions such as mergeSort and quickSort would need to be rewritten to sort values of the new type. In constrast, here is the type of mergeSort in Haskell mergeSort :: Ord a => [a] -> [a] Er, you could define mergesort using templates in c++ and it would work pretty much the same.

How do you express the Ordinality constraint in C++?

Concepts will accomplish this when they finally make it into the standard.

Re: Haskell tutorial for C programmers

#8
post #4

>What this means is that existing sort functions such as mergeSort and quickSort would need to be rewritten to sort values of the new type. In constrast, here is the type of mergeSort in Haskell mergeSort :: Ord a => [a] -> [a] Er, you could define mergesort using templates in c++ and it would work pretty much the same.

How do you express the Ordinality constraint in C++?

The existence of an operatorInterfaces or pure virtual classes are not the same because they're closed.

Re: Haskell tutorial for C programmers

#9

Earlier quoted context omitted.

How do you express the Ordinality constraint in C++?

Concepts will accomplish this when they finally make it into the standard.

Concepts will certainly be nice once they arrive in the standard, but it is already possible today with some trickery. Example checking whether an std::vector's type is comparable with `operatorhttp://coliru.stacked-crooked.com/a/2ea46e0afd894d3d

The error you get if you try to sort a vector with an unordered type is relatively easy to read:

    main.cpp: In instantiation of 'void merge_sort(std::vector&) [with T = type]':
    main.cpp:45:17:   required from here
    main.cpp:30:5: error: static assertion failed: Element type is not sortable
         static_assert( concept::is_ord(), "Element type is not sortable" );
         ^
Post reply on HN