Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell function to cast as a type

Tags:

types

haskell

If I want a version of reads that returns a list of (Int, String), the example I see is

f x = reads x :: [(Int,String)]

I'd like to know if there's a way to do this in point-free style (f = g . reads?), or if this is something you just don't/can't do in Haskell.

I haven't seen any examples of using a type as an argument, so it may not be doable.

like image 512
beardc Avatar asked Jun 11 '14 15:06

beardc


2 Answers

I think what you're looking for is f = reads :: String -> [(Int, String)].

like image 132
Ethan Lynn Avatar answered Nov 05 '22 17:11

Ethan Lynn


The idiomatic way to do what you're describing is

f :: String -> [(Int, String)]
f = reads

The benefit to doing it this way is if you have a polymorphic type the result is what you'd expect (because of the Dreaded Monomorphism Restriction).

like image 30
Dan Avatar answered Nov 05 '22 17:11

Dan