Custom types and their usage [closed] - haskell

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Hello I have 3 custom types and then type created with these 3 types.
type Name = String
type Age = Int
type Semester = Int
type Student = (Name, Age, Semester)
I need to create function which takes student and returns his name
i have created this but it doesn't work.
getName :: Student -> Name:
getName (name_, age_, semester_) = name_

This works fine:
getName :: Student -> Name
getName (name_, age_, semester_) = name_
There shouldn't be any : or anything else in the end of the signature.
First line contains the signature by itself. Then go the lines with the definition. The type alias is Name, not Name:, so that's what should be used.

Related

Haskell convert list of list of strings to string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm currently trying to pretty-print a list of list of strings as a single string. My current code is
pretty :: [[String]]->String
pretty x
= concat(concat(x))
main :: IO ()
main
= putStrLn (show (pretty [["hi","hi","hi"]]))
When asked to do pretty [["hi","hi","hi"]], it converts it to "hihihi" but when asked pretty [["hi","hi","hi"]["hi","hi","hi"]] the compiler gives the following error
main.hs:7:29: error:
* Couldn't match expected type `[[Char]] -> [String]'
with actual type `[[Char]]'
* The function `["hi", "hi", "hi"]' is applied to one argument,
but its type `[[Char]]' has none
In the expression: ["hi", "hi", "hi"] ["hi", "hi", "hi"]
In the first argument of `pretty', namely
`[["hi", "hi", "hi"] ["hi", "hi", ....]]'
|
7 | = putStrLn (show (pretty [["hi","hi","hi"]["hi","hi","hi"]]))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Never mind guys. It turned out I forgot the comma in [["hi","hi","hi"],["hi","hi","hi"]]

Non.exhaustive pattern [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So im creating a function that says true if a is equal to the fst element of any pair in a list
elemMSet :: Eq a => a -> [(a,Int)] -> Bool
elemMset a [] = False
elemMSet a ((t,q):xs)| a==t = True
| otherwise = elemMSet a xs
I dont undertstand why, it shows an error of non-exhaustive pattern when i try something that should give False like :
elemMSet 'd' [('b',2), ('a',4), ('c',1)]
Error:
Tseis.hs:(4,1)-(5,48): Non-exhaustive patterns in function elemMSet
You misspelled the function name on line 2, so elemMSet only covers the non-empty case. Change the name on line 2 to elemMSet (with a capital S) and it will work fine.

Haskell Reversed Number Digit List [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new to Haskell and I am trying to turn an int into a reversed digit list(of ints).
What I have is:
Lnat 0 = [0]
Lnat x = [mod x 10] ++ Lnat (div x 10)
However I get the error "Not in scope: data constructor 'Lnat'" on both lines and it crashes trying to load the file.
Could you please explain the root of this and how to fix it?
All values must start with a lowercase character. If it starts with a capital or : then that value is a data constructor, to be used in data declarations. This is what you'll want to change your function to:
lnat 0 = [0]
lnat x = mod x 10 : lnat (div x 10)
Note that I also changed the inefficient ++ operator to : to add a bit more speed.

How to call a function returning a tuple in another function in haskell? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have two functions like these:
notice_objects_at::String -> IO()
notice_objects_at place = do
let (X,Y) = at place
putStrLn ("There is a" ++ show X ++ "," ++ show Y ++ "here.")
putStrLn "Hi"
at::String-> (String, String)
at place =
case place of
"bedroom" -> ("fly", "light switch")
"den" -> ("flyswatter", "light switch")
from the 'at' function I am returning a tuple, which I want to store to two variables X and Y in the notice_objects_at function. But, I'm getting an error that:
Not in scope: data constructor ‘X’
Not in scope: data constructor ‘Y’
Not in scope: data constructor ‘X’
Not in scope: data constructor ‘Y’
What is wrong?
Haskell syntax relies on the capitalisation of names. As described here:
Anything that starts with a capital letter is either a concrete type
or a data constructor. Lower-case-starting names are reserved for
function names and variables, including type variables.
So when you bind names to the elements of the tuple in:
let (X,Y) = at place
you need lowercase names:
let (x,y) = at place
(and adjust the names wherever else they are used, of course!)
Otherwise Haskell interprets these names as data constructors, but of course cannot find their definition anywhere, hence your error messages.
See also Why does Haskell force data constructor's first letter to be upper case?

Haskell: Type to represent a sudoku line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In Haskell, how can I create a type to represent a list of length 9 which each elements are an Int between 0 and 9?
You could use smart constructors:
module Sudoku(SudokuSquare, sudokuSquare) where
import Data.Traversable(traverse)
data SudokuSquare = SSquare Int
sudokuSquare :: Int -> Maybe SudokuSquare
sudokuSquare i = if i >= 0 && i <= 9 then Just (SSquare i) else Nothing
buildRow :: [Int] -> Maybe [SudokuSquare]
buildRow = traverse sudokuSquare

Resources