Ok so I have an assessment, first part is:
Consider the following declaration for a parameterised type of lists in which
Nil represents the empty list, and Cons x xs represent a non-empty list with
first element x and remaining list of elements xs:
data List a = Nil | Cons a (List a)
Am I right in reading it like: Declare a List type which can take any parameter and is constructed by Nil or Cons which itself takes anything and a List of anything?
Also it seems strange to define a List without [] or : like in the prelude but I assume it’s just for the purposes of the question. Is the (List a) used just to make the order of evaluation explicit or is it some weird thing with tuples?
Ok so the actual question is:
Q) Define a recursive function append :: List a -> List a -> List a that
appends two lists together to give a single list.
So I’d have to do something similar to how append is done in the standard prelude I.e.
(++) :: [a] -> [a] -> [a]
[] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)
How do I do pattern matching on a user defined type?
I was thinking something like:
Nil append List a = List a
For the first line but the second line is confusing me. I’ve read some stuff on pattern matching for data constructors but I don’t know how relevant it is here.
Maybe something like this would work:
Cons a (List a) append List b = a : (List a append List b)
But I’m getting ‘Data constructor not in scope’ for everything to do with List...it thinks it’s a data constructor although I’ve defined it as a type above.
Any help would be great 😊
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Hi,
For what it's worth, Daniel's suggestion of:
```haskell
isAlphabetic char = elem char ['A'..'Z']
```
does read nicer but would have to traverse `['A'..'Z']` every time. I think
what you have is fine, and although relational and boolean operators are
also in imperative languages, they behave as pure functions even in
imperative languages if subexpressions don't cause side effects. I don't
think it's unidiomatic, and especially in this case, "between A and Z"
means the same thing as "is one of the letters A, B, C...Z", so the intent
of the function is clear as written.
Best of all would probably be using `isAlpha` from `Data.Char` (in `base`).
Best,
toz
On Wed, May 20, 2020 at 2:41 PM <beginners-request(a)haskell.org> wrote:
> Send Beginners mailing list submissions to
> beginners(a)haskell.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> or, via email, send a message with subject or body 'help' to
> beginners-request(a)haskell.org
>
> You can reach the person managing the list at
> beginners-owner(a)haskell.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Beginners digest..."
>
>
> Today's Topics:
>
> 1. Code Review of Caesar-Cipher (chrysaetos99)
> 2. Re: Code Review of Caesar-Cipher (Daniel van de Ghinste)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 20 May 2020 20:26:37 +0200
> From: chrysaetos99 <chrysaetos99(a)posteo.de>
> To: beginners(a)haskell.org
> Subject: [Haskell-beginners] Code Review of Caesar-Cipher
> Message-ID: <cd08aa1e-1c9b-a609-15df-5bf37508cb96(a)posteo.de>
> Content-Type: text/plain; charset="utf-8"; Format="flowed"
>
> Background
> ----------
> I am a total beginner in Haskell, so after reading the "Starting
> out"-chapter of "Learn you a Haskell", I wanted to create my first
> program that actually does something.
>
> I decided to do the famous Caesar-Cipher.
>
>
> Code
> ----
> See attachment.
>
>
> Question(s)
>
> -----------
>
> - How can this code be improved in general?
> - Do I follow the style guide of Haskell (indentation, etc.)?
> - I have a background in imperative languages. Did I do something that
> is untypical for functional programming languages?
>
> I would appreciate any suggestions.
>
> ---
>
> Please note: I also asked this question on
>
> https://codereview.stackexchange.com/questions/242529/caesar-cipher-impleme…,
>
> but didn't receive an answer that really answered all my questions.
>
>
> Kind regards
>
> chrysaetos99
>
>
>
Background
----------
I am a total beginner in Haskell, so after reading the "Starting
out"-chapter of "Learn you a Haskell", I wanted to create my first
program that actually does something.
I decided to do the famous Caesar-Cipher.
Code
----
See attachment.
Question(s)
-----------
- How can this code be improved in general?
- Do I follow the style guide of Haskell (indentation, etc.)?
- I have a background in imperative languages. Did I do something that
is untypical for functional programming languages?
I would appreciate any suggestions.
---
Please note: I also asked this question on
https://codereview.stackexchange.com/questions/242529/caesar-cipher-impleme…,
but didn't receive an answer that really answered all my questions.
Kind regards
chrysaetos99
Hi,
safeHead.hs
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
prelude> safeHead [23,1,4,2,4,2]
Just 23
Question:
what am i doing wrong?
best,
Alexander
Hi,
tensDigit.hs
tensDigit :: Integral a => a ->a
tensDigit x = d
where xLast = x `div` 10
d = xLast `mod` 10
prelude> load tensDigit.hs
parse error on input ‘=’
Perhaps you need a 'let' in a 'do' block?
e.g. 'let x = 5' instead of 'x = 5'
4 | d = xLast `mod` 10 | ^
[1 of 1] Compiling Main ( chapter7_8.hs, interpreted )
Failed, no modules loaded.
I suspect this has something to do with the spacing (since this is example code) if so could someone explain to me what the rules on spacing are so I can get past this. I am using a simple text editor so if someone has a recommendation for an IDE that simply takes care of this, much obliged!!
best,
> I am using a simple text editor
> so if someone has a recommendation for an IDE that simply takes care of this, much obliged!!
Geany is a powerful, stable and lightweight programmer's text editor that provides tons of useful features without bogging down your workflow. It runs on Linux, Windows and MacOS is translated into over 40 languages, and has built-in support for more than 50 programming languages.
Hi,
superclass.hs
myX = 1 :: Int
sigmund :: Int -> Int
sigmund x = myX
prelude> :l superclass.hs
[1 of 1] Compiling Main
Ok, one module loaded.
==================================================================================
superclass.hs
myX = 1 :: Int
sigmund :: Num -> Num
sigmund x = myX
prelude> :l superclass.hs
typed_checked.hs:3:13: error:
• Expecting one more argument to ‘Num’
Expected a type, but ‘Num’ has kind ‘* -> Constraint’
• In the type signature: sigmund :: Num -> Num
|
3 | sigmund :: Num -> Num | ^^^
typed_checked.hs:3:20: error:
• Expecting one more argument to ‘Num’
Expected a type, but ‘Num’ has kind ‘* -> Constraint’
• In the type signature: sigmund :: Num -> Num
===================================================================================
I would think since Num is a superclass of Int defining the type in the superclass would be OK, also in the error message it refers to (see black) what does that refer to?
thanks!
Hi,
binder.hs
i:: Num a => a
prelude>:l binder.hs
typed_checked.hs:1:1: error:
The type signature for ‘i’ lacks an accompanying binding
|
1 | i:: Num a=> a | ^
binder.hs
i:: Num a => a
i = 2
prelude>:l binder.hs
[1 of 1] Compiling Main
Ok, one module loaded.
Why does it need a binder to make it work?
Unsubscribe
Sent from my iPad
> On Apr 26, 2020, at 8:51 AM, Ken Overton <ken.overton(a)gmail.com> wrote:
>
>
> Hello all,
>
> I recently came across this function which made me realize I don't understand list comprehensions well. I hope someone can help me understand them better by understanding this example better. The function takes a list of Eq and returns the list of unique elements from it:
>
> unique :: Eq a => [a] -> [a]
> unique xs = [x | (x,y) <- zip xs [0..], x `notElem` (take y xs)]
>
> It's using a list comprehension with multiple 'generators' (hope I have the term correctly). My understanding of multiple generators in a list comprehension is that they refine the results of the previous generator.
>
> So the first generator should produce [(Eq,Int)] as input to the second generator? And the second generator should produce [Bool]?
>
> My understanding must be wrong though; how do we end up with just the items where the second generator produced True?
>
> Thanks,
>
>
> --
> Ken Overton
> (917) 863-3937
> ken.overton(a)gmail.com
>
> _______________________________________________
> Beginners mailing list
> Beginners(a)haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Hello all,
I recently came across this function which made me realize I don't
understand list comprehensions well. I hope someone can help me understand
them better by understanding this example better. The function takes a list
of Eq and returns the list of unique elements from it:
unique :: Eq a => [a] -> [a]
unique xs = [x | (x,y) <- zip xs [0..], x `notElem` (take y xs)]
It's using a list comprehension with multiple 'generators' (hope I have the
term correctly). My understanding of multiple generators in a list
comprehension is that they refine the results of the previous generator.
So the first generator should produce [(Eq,Int)] as input to the second
generator? And the second generator should produce [Bool]?
My understanding must be wrong though; how do we end up with just the items
where the second generator produced True?
Thanks,
--
Ken Overton
(917) 863-3937
ken.overton(a)gmail.com